Hibernate對(duì)集合屬性的操作
持久化對(duì)象的映射集合屬性:Set List Collection Map SortedSet SortedMap 數(shù)組
集合屬性對(duì)應(yīng)的元素有 set list (bag idbag) map set map array
集合屬性默認(rèn)采用懶加載
集合屬性被從一個(gè)持久化對(duì)象傳遞到另一個(gè)持久化對(duì)象棚潦,集合元素對(duì)應(yīng)的記錄會(huì)從一個(gè)表轉(zhuǎn)移到另一個(gè)表
兩個(gè)持久化對(duì)象不能共享同一個(gè)集合元素的引用
映射索引列元素有: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 關(guān)聯(lián)映射
Hibernate 關(guān)系映射 總結(jié)整理
-
1 to 1
- 基于外鍵的單向 1 to 1
public class Account { private Integer id; private String name; //主控端引用被控端的對(duì)象 private Address address; } public class Address { private int id; private String address; } Account.hbm.xml: // 基于外鍵的單向一對(duì)一實(shí)際上是多對(duì)一關(guān)聯(lián)映射的特例 // 采用<many-to-one>標(biāo)簽绕沈,指定多的一端的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; //主控端引用被控端的對(duì)象 private Address address_id; } public class Address { private int id; private String address; //聲明一個(gè)對(duì)主控端對(duì)象的引用 private Account account; } Account.hbm.xml: <many-to-one name="address_id" column="address_id" unique="true"></many-to-one> Address.hbm.xml: // 基于外鍵的雙向一對(duì)一關(guān)聯(lián)映射需要在一端添加<one-to-one>標(biāo)簽,用property-ref來(lái)指定反向?qū)傩砸?<one-to-one name="account" property-ref="address_id"></one-to-one>
- 基于主鍵的單向 1 to 1
public class IDCard { private Integer id; private String no; //主控端引用被控端的對(duì)象 private Citizen citizen; } public class Citizen { private int id; private String name; } IDCard.hbm.xml: <!-- constrained 告訴當(dāng)前主鍵灶伊,你的值時(shí)采用另個(gè)表中的主鍵的值 當(dāng)前主鍵對(duì)于有關(guān)系的另一個(gè)表來(lái)說(shuō)就是外鍵弥鹦。 --> <one-to-one name="citizen" constrained="true"></one-to-one>
- 基于主鍵的雙向 1 to 1
public class IDCard { private Integer id; private String no; //主控端引用被控端的對(duì)象 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: <!-- 建立一對(duì)一關(guān)系 --> <one-to-one name="idCard" />
- 基于外鍵的單向 1 to 1
-
單向 1 to many
單向一對(duì)多關(guān)聯(lián)映射亲配,在對(duì)象關(guān)系映射文件中使用<one-to-many>標(biāo)簽映射,開(kāi)發(fā)中不常見(jiàn) 在數(shù)據(jù)庫(kù)中表的關(guān)系為:多端有一個(gè)字段為外鍵指向一端 public class Account { private int id; private String accName; //對(duì)多端對(duì)象集合的引用 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
單向多對(duì)一關(guān)聯(lián)中對(duì)象模型中類(lèi)之間的引用在關(guān)系模型中表示為表之間的外鍵引用惶凝,通過(guò)<many-to-one>標(biāo)簽映射多對(duì)一關(guān)聯(lián) // 少的一端 public class Dept { private int id; private String deptName; } // 多的一端 public class Employee { private int id; private String empName; private Date hiredate; //對(duì)一端的引用 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; //對(duì)多端對(duì)象集合的引用 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 ) 自身關(guān)聯(lián)
public class Category { private int id; private String name; //如果把Category看成是多的一端 private Category parent; //如果把Category看成是少的一端,則需要對(duì)多的一端進(jìn)行對(duì)象集合的引用 private Set<Category> clist; } Category.hbm.xml: <set name="clist" inverse="true"> <key column="parent_id"></key> <!-- 配置一對(duì)多的關(guān)聯(lián)映射 --> <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 ) 自身關(guān)聯(lián)
-
many to many
在關(guān)系模型中,無(wú)法直接表達(dá)兩個(gè)表之間的多對(duì)多關(guān)系犬钢。需要?jiǎng)?chuàng)建一個(gè)連接表苍鲜,它同時(shí)參照兩個(gè)表 public class Course { private int id; private String name; //對(duì)學(xué)生集合的引用 private Set<Student> stuList; } public class Student { private int id; private String name; private String gender; //對(duì)課程集合的引用 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>
分別進(jìn)行 雙向 1 to many ( many to 1 ) public class Course02 { private int id; private String name; //對(duì)學(xué)生集合的引用 private Set<StuCourse02> stuList; } public class Student02 { private int id; private String name; private String gender; //對(duì)課程集合的引用 private Set<StuCourse02> courseList; } // 學(xué)生和課程的關(guān)系實(shí)體 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中關(guān)聯(lián)映射的最佳實(shí)踐
- 為每個(gè)持久實(shí)體類(lèi)寫(xiě)一個(gè)映射文件
- 不要用怪異的連接映射
- 偏愛(ài)雙向關(guān)聯(lián)