第九章 关系映射 一对多关联映射
来源:互联网 发布:长歌门成男捏脸数据 编辑:程序博客网 时间:2024/05/16 16:56
如: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)
- 第九章 关系映射 一对多关联映射
- 映射一对多关联关系
- 映射一对多关联关系
- 映射一对多关联关系
- 第九章 关系映射 组件关联映射
- 第九章 关系映射 集合关联映射
- 第九章 关系映射 继承关联映射
- 第九章 关系映射 多对多关联映射
- 第九章 关系映射 多对一关联映射
- 06映射一对多双向关联关系
- 关联映射的关系(一对多)
- Hibernate 映射一对多关联关系
- hibernate一对多关系映射(自身关联)
- hibernate 一对多 关系关联映射
- hibernate映射一对多双向关联关系
- hibernate的映射:一对多关联关系
- 5.映射一对多关联关系
- 1010--映射一对多关联关系
- 让Mac OS X的终端多姿多彩
- NDK简介
- 网络爬虫浅析
- linux 环境变量配置及查找字符串文件方法
- 散谈Windows内存分配函数
- 第九章 关系映射 一对多关联映射
- CSS学习笔记
- Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.MDC.getCopyOfContextMap()Ljava/uti
- vc 中bsc文件
- 教你正确了解电脑配置【乡巴佬】
- 智力题(5)数学
- android:launchMode
- jquery 实现简单的切图
- HDU 2037 今年暑假不AC