Hibernate对集合属性的操作
持久化对象的映射集合属性:Set List Collection Map SortedSet SortedMap 数组
集合属性对应的元素有 set list (bag idbag) map set map array
集合属性默认采用懒加载
集合属性被从一个持久化对象传递到另一个持久化对象,集合元素对应的记录会从一个表转移到另一个表
两个持久化对象不能共享同一个集合元素的引用
映射索引列元素有:list-index / map-key / map-key-many-to-many / composite-map-key
映射集合元素的元素有:element / composite-element / one-to-many / many-to-many
-
Set集合属性操作
<set name="hobby" table="hobby_tab"> <key column="student_id"></key> <element type="string" column="hobby"></element> </set>
-
List集合属性操作
<list name="hobby" table="hobby_tab_list"> <key column="student_id"></key> <list-index column="position"></list-index> <element type="string" column="hobby"></element> </list>
-
Collection集合属性操作
private Collection<String> hobby;
<idbag name="hobby" table="hobby_tab_c"> <collection-id type="string" column="hobby_id"> <generator class="uuid"></generator> </collection-id> <key column="student_id"></key> <element type="string" column="hobby"></element> </idbag>
-
Map集合属性操作
<map name="hobby" table="stu_map_hobby"> <key column="student_id"></key> <map-key column="keycolumn" type="string"></map-key> <element type="string" column="hobby"></element> </map>
Hibernate 关联映射
-
1 to 1
- 基于外键的单向 1 to 1
public class Account { private Integer id; private String name; //主控端引用被控端的对象 private Address address; } public class Address { private int id; private String address; } Account.hbm.xml: // 基于外键的单向一对一实际上是多对一关联映射的特例 // 采用<many-to-one>标签,指定多的一端的unique=true,这样就限制了多端的多重性为一 <many-to-one name="address" column="address_id" unique="true"></many-to-one>
- 基于外键的双向 1 to 1
public class Account { private Integer id; private String name; //主控端引用被控端的对象 private Address address_id; } public class Address { private int id; private String address; //声明一个对主控端对象的引用 private Account account; } Account.hbm.xml: <many-to-one name="address_id" column="address_id" unique="true"></many-to-one> Address.hbm.xml: // 基于外键的双向一对一关联映射需要在一端添加<one-to-one>标签,用property-ref来指定反向属性引用 <one-to-one name="account" property-ref="address_id"></one-to-one>
- 基于主键的单向 1 to 1
public class IDCard { private Integer id; private String no; //主控端引用被控端的对象 private Citizen citizen; } public class Citizen { private int id; private String name; } IDCard.hbm.xml: <!-- constrained 告诉当前主键,你的值时采用另个表中的主键的值 当前主键对于有关系的另一个表来说就是外键。 --> <one-to-one name="citizen" constrained="true"></one-to-one>
- 基于主键的双向 1 to 1
public class IDCard { private Integer id; private String no; //主控端引用被控端的对象 private Citizen citizen; } public class Citizen { private int id; private String name; private IDCard idCard; } IDCard.hbm.xml: <one-to-one name="citizen" constrained="true"></one-to-one> Citizen.hbm.xml: <!-- 建立一对一关系 --> <one-to-one name="idCard" />
- 基于外键的单向 1 to 1
-
单向 1 to many
单向一对多关联映射,在对象关系映射文件中使用<one-to-many>标签映射,开发中不常见 在数据库中表的关系为:多端有一个字段为外键指向一端 public class Account { private int id; private String accName; //对多端对象集合的引用 private Set<Orders> setOrders; } public class Orders { private int id; private String orderNum; private Date orderTime; } Account.hbm.xml: <set name="setOrders"> <key column="acc_id"></key> <one-to-many class="com.jikexueyuan.entity.Orders"/> </set>
-
单向 many to 1
单向多对一关联中对象模型中类之间的引用在关系模型中表示为表之间的外键引用,通过<many-to-one>标签映射多对一关联 // 少的一端 public class Dept { private int id; private String deptName; } // 多的一端 public class Employee { private int id; private String empName; private Date hiredate; //对一端的引用 private Dept dept; } Employee.hbm.xml: <many-to-one name="dept" column="dept_id"></many-to-one>
-
双向 1 to many ( many to 1 )
public class Account { private int id; private String accName; //对多端对象集合的引用 private Set<Orders> setOrders; } public class Orders { private int id; private String orderNum; private Date orderTime; private Account account; } Account.hbm.xml: <set name="setOrders" cascade="all" inverse="true"> <key column="acc_id"></key> <one-to-many class="com.jikexueyuan.entity.Orders"/> </set> Orders.hbm.xml: <many-to-one name="account" column="acc_id"></many-to-one>
- 双向 1 to many ( many to 1 ) 自身关联
public class Category { private int id; private String name; //如果把Category看成是多的一端 private Category parent; //如果把Category看成是少的一端,则需要对多的一端进行对象集合的引用 private Set<Category> clist; } Category.hbm.xml: <set name="clist" inverse="true"> <key column="parent_id"></key> <!-- 配置一对多的关联映射 --> <one-to-many class="com.jikexueyuan.entity.Category"/> </set> <many-to-one name="parent" column="parent_id"></many-to-one>
- 双向 1 to many ( many to 1 ) 自身关联
-
many to many
在关系模型中,无法直接表达两个表之间的多对多关系。需要创建一个连接表,它同时参照两个表 public class Course { private int id; private String name; //对学生集合的引用 private Set<Student> stuList; } public class Student { private int id; private String name; private String gender; //对课程集合的引用 private Set<Course> courseList; } Course.hbm.xml: <set name="stuList" table="stu_course01"> <key column="course_id"></key> <many-to-many column="student_id" class="com.jikexueyuan.entity1.Student"></many-to-many> </set> Student.hbm.xml: <set name="courseList" table="stu_course01"> <key column="student_id"></key> <many-to-many column="course_id" class="com.jikexueyuan.entity1.Course"></many-to-many> </set>
分别进行 双向 1 to many ( many to 1 ) public class Course02 { private int id; private String name; //对学生集合的引用 private Set<StuCourse02> stuList; } public class Student02 { private int id; private String name; private String gender; //对课程集合的引用 private Set<StuCourse02> courseList; } // 学生和课程的关系实体 public class StuCourse02 { private int id; private double score; private Student02 stu; private Course02 course; } Course02.hbm.xml: <set name="stuList"> <key column="course_id"></key> <one-to-many class="com.jikexueyuan.entity2.StuCourse02"/> </set> Student02.hbm.xml: <set name="courseList"> <key column="student_id"></key> <one-to-many class="com.jikexueyuan.entity2.StuCourse02"/> </set> StuCourse02.hbm.xml: <many-to-one name="stu" column="student_id"/> <many-to-one name="course" column="course_id"/>
-
Hibernate中关联映射的最佳实践
- 为每个持久实体类写一个映射文件
- 不要用怪异的连接映射
- 偏爱双向关联