如:department与employee
employee中有一个department_id的外键
Department:
public class Department implements Serializable {
private Integer id;
private String name;
private Set<Employee> employees;
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public Department() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Employee:
public class Employee implements Serializable {
private Integer id;
private String name;
public Employee() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Department.hbm.xml:(与普通的映射文件一样)
<hibernate-mapping>
<class name="cn.framelife.hibernate.entity.Department" table="department"
catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
<set name="employees" inverse="false" cascade="all">
<key column="department_id"></key>
<one-to-many class="cn.framelife.hibernate.entity.Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml:
<hibernate-mapping>
<class name="cn.framelife.hibernate.entity.Employee" table="employee"
catalog="hibernate">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>
增加:
transaction = session.beginTransaction();
Department department = new Department();
department.setName("zzza");
Set<Employee> set = new HashSet<Employee>();
for(int i=0; i < 5; i++){
Employee employee = new Employee();
employee.setName("zz"+i);
set.add(employee);
}
department.setEmployees(set);
session.save(department);
transaction.commit();
查询:
查询department的时候可以得到外键关联的所有employee对象。
注意事项:
a、错误提示:Field'department_id'
doesn't have a default value
数据表中把"department_id"设成可以为空的,但是Hibernate先执行的是:
因为hibernate执行的顺序是这样的:
Hibernate:insert into hibernate.department (name) values (?)
Hibernate:insert into hibernate.employee (name) values (?)
Hibernate:insert into hibernate.employee (name) values (?)
Hibernate:insert into hibernate.employee (name) values (?)
Hibernate:insert into hibernate.employee (name) values (?)
Hibernate:insert into hibernate.employee (name) values (?)
Hibernate:update hibernate.employee set department_id=? where id=?
Hibernate:update hibernate.employee set department_id=? where id=?
Hibernate:update hibernate.employee set department_id=? where id=?
Hibernate:update hibernate.employee set department_id=? where id=?
Hibernate:update hibernate.employee set department_id=? where id=?
department_id外键是作为一个后来才更新的存在。所有不能设置为非空的。
b、Department.hbm.xml中要设置cascade="all"(级联),或其它有效值,不然,在保存Department对象时,相关的Employee对象不会被保存。(none,all,save-update,delete..)
c、Department.hbm.xml中set标签的inverse属性不能设置为"true"(放弃维护关联关系),inverse的默认值是"false",所以不加inverse也可以。看书上说:在一对多的关联关系实现中,最好设置inverse="true",将有助于性能的改善。所以一开始就用了inverse="true",User和Card对象都分别正确写入数据库了,但是就是departmentID字段没有被自动写入。
d、多对一与一对多可以一起用,形成双向关系。
多对一映射的使用:http://blog.csdn.net/p_3er/article/details/9036759
e.one-to-many、many-to-one、many-to-many懒加载分析:
必须同时满足下面的两个条件时才能实现懒散加载:
1).lazy!=false(lazy缺省方式就!=false,lazy是=proxy)
2).fetch=select(fetch缺省方式即为select)
分享到:
相关推荐
hihernate一对多关联映射(双向Classes<----->Student)(常用) 14 hibernate一对多双向自连接关联映射 15 hibernate多对多关联映射(单向User---->Role) 19 hibernate多对多关联映射(双向User<---->Role) 20 ...
12.4.6 一对多双向关联的annotation注解实现540 12.4.7 一对多双向自身关联的annotation注解实现542 12.4.8 多对多单向关联的annotation注解实现543 12.4.9 多对多双向关联的annotation注解实现545 12.4.10 组成关系...
第7章 映射一对多关联关系 7.1 建立多对一的单向关联关系 7.1.1 元素的not-null属性 7.1.2 级联保存和更新 7.2 映射一对多双向关联关系 7.2.1 元素的inverse属性 7.2.2 级联删除 7.2.3 父子关系 7.3...
第7章 映射一对多关联关系 7.1 建立多对一的单向关联关系 7.1.1 元素的not-null属性 7.1.2 级联保存和更新 7.2 映射一对多双向关联关系 7.2.1 元素的inverse属性 7.2.2 级联删除 7.2.3 父子关系 7.3...
第7章 映射一对多关联关系 7.1 建立多对一的单向关联关系 7.1.1 元素的not-null属性 7.1.2 级联保存和更新 7.2 映射一对多双向关联关系 7.2.1 元素的inverse属性 7.2.2 级联删除 7.2.3 父子关系 7.3...
第7章 映射一对多关联关系 7.1 建立多对一的单向关联关系 7.1.1 元素的not-null属性 7.1.2 级联保存和更新 7.2 映射一对多双向关联关系 7.2.1 元素的inverse属性 7.2.2 级联删除 7.2.3 父子关系 7.3...
一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 <idbag> 14 双向关联(Bidirectional Associations) 14 三重关联(Ternary ...
第一章 SELECT查询 写一条SELECT查询语句 在查询中使用表达式、运算符 对空值的处理 ...第九章 对数据的操作 在已创建表中插入新的数据 修改已经存在的数据 删除表中的数据 理解事物控制及其重要性 等等......
7.2.1 一对多关联 7.2.2 多对多关联 7.2.3 把列添加到联结表 7.2.4 映射map 7.3 多态关联 7.3.1 多态的多对一关联 7.3.2 多态集合 7.3.3 对联合的多态关联 7.3.4 每个具体类一张...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
一、NHibernate概括 二、NHibernate环境与结构体系 三、第一个NHibernate应用程序 四、持久对象的生命周期(上) 五、持久对象的生命周期(下) 六、基本映射 ...九、一对多关联映射 十、多对多关联映射
第一部分 客观选择题 2 第1章 绪论 2 第2章 关系数据库 3 第3章 关系数据库标准语言SQL 5 第5章 关系数据理论 7 第6章 数据库设计 9 第7章 数据库恢复技术 11 第8章 并发控制 12 第9章 数据库安全性 13 第10章 ...
第30部分 继承与建模高级应用之多对多关联 第31部分 继承与建模高级应用之自引用关联 第32部分 继承与建模高级应用之TPH与TPT (1) 第33部分 继承与建模高级应用之TPH与TPT (2) 第34部分 继承与建模高级应用之多条件...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...