第九章 关系映射 一对多关联映射
2013-06-06 10:54
381 查看
如: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:
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:
增加:
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)
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)
相关文章推荐
- 第九章 关系映射 继承关联映射
- 第九章 关系映射 多对一关联映射
- 第九章 关系映射 多对多关联映射
- 第九章 关系映射 集合关联映射
- Hibernate关系映射(4)_一对一双向主键关联
- Hibernate关系映射级别注解(多对多单向外键关联、多对多双向外键关联)
- ORM对象关系映射之GreenDAO建立多表关联
- Hibernate关系映射(六)多对多单向关联@ManyToMany Annotation方式
- Hibernate关系映射(5)_一对一单向联合主键关联
- hibernate映射关系 一对多、多对一单向关联 一对多双向关联
- Hibernate 、多表关联映射-多对一关系(many-to-one)
- 【HIbernate框架学习】:Hibernate对象关系映射之单向一对多关联映射
- hibernate 关系映射之 双向外键关联一对一
- Hibernate 映射关系 ---One2One 主键关联
- Hibernate 映射关系 ---Many2Many 双向关联
- Hibernate的七种映射关系之七种关联映射(一)
- 在java-Hibernate关系映射之关联映射知识描述
- ORM对象关系映射之GreenDAO建立多表关联
- Hibernate 关系映射(7) 基于外键关联的单向1:N
- 【HIbernate框架学习】:Hibernate对象关系映射之一对一关联映射(二)