1>:one-to-one(一對一關聯)主鍵關聯:
一對一關聯一般可分為主鍵關聯和外鍵關聯
主鍵關聯的意思是說關聯的兩個實體共享一個主鍵值,但這個主鍵可以由兩個表產生.
現在的問題是:
*如何讓另一個表引用已經生成的主鍵值
解決辦法:
*Hibernate映射文件中使用主鍵的foreign生成機制
eg:學生表:
<hibernate-mapping>
<class table="user" catalog="study">
<id type="java.lang.Integer">
<column />
<generator />
</id>
<property type="java.lang.String">
<column length="20" />
</property>
<one-to-one cascade="all"></one-to-one>
</class>
</hibernate-mapping>
添加:<one-to-one
fetch="join"
cascade="all" />
<class>元素的lazy屬性為true,表示延遲加載,如果lazy設為false,則表示立即加載.以下對這二點進行說明.
立即加載:表示在從數據庫中取得數據組裝好一個對象后,會立即再從數據庫取得數據組裝此對象所關聯的對象
延遲加載:表示在從數據庫中取得數據組裝好一個對象后,不會立即從數據庫中取得數據組裝此對象所關聯的對象,
而是等到需要時,才會從數據庫取得數據組裝此關聯對象.
<one-to-one>元素的fetch屬性可選為select和join
join:連接抓取,Hibernate通過在Select語句中使用outer join(外連接)來獲得對象的關聯實例或者關聯集合.
select:查詢抓取,Hibernate需要另外發送一條select語句抓取當前對象的關聯實體或集合.
******所以我們一般用連接抓取<join>
證件表:
<hibernate-mapping>
<class table="card" lazy="true" catalog="study">
<id type="java.lang.Integer">
<column />
<generator >
<param >user</param>
</generator>
</id>
<!-- id使用外鍵(foreign)生成機制,引用代號為user的對象
的主鍵作為card表的主鍵和外鍵。同時user在下面的
<one-to-one>中進行了定義 -->
<property type="java.lang.Integer">
<column />
</property>
<one-to-one constrained="true"></one-to-one>
<!-- constrained="true"表示card引用了student的主鍵作為外鍵。 -->
</class>
</hibernate-mapping>
一對一映射必須加載的元素有:
name
class
constrained(主鍵關聯)
fetch(關聯的一方才有:意思是說需要通過這一方查詢另一方數據的一方.比如學生表查詢證件中的數據,學生就是關聯的一方)
cascade(關聯的一方才有:意思是說需要通過這一方保存或者更新數據對另一方也產生影響(另一方也保存或者更新了,比如保存學生信息,那么與學生相關聯的證件信息也保存了))
2><one-to-one>外鍵關聯:
開發中可以參照<one-to-one>主鍵關聯和<many-to-one>
這里,學生表保存不變,只改變證件表:
<hibernate-mapping>
<class table="card" lazy="true">
<id type="java.lang.Integer">
<column />
<generator /><!-- 不再是foreign了,因為它的主鍵不是學生表的主鍵,它的主鍵是自動產生的,它的外鍵才是學生表的主鍵 -->
</id>
<property type="java.lang.String">
<column length="20" />
</property>
<many-to-one column="userid" unique="true"/>
<!-- 惟一的多對一,如果被限制為唯一,實際上變成一對一關系了 -->
<!-- unique設為true表示使用DDL為外鍵字段生成一個惟一約束。
以外鍵關聯對象的一對一關系,其本質上已經變成了一對多的雙向關聯,
應直接按照一對多和多對一的要求編寫它們的映射文件。當unique為
true時實際上變成了一對一的關系。
***這里需要強調一點的是<many-to-one>元素必須有column這一項,它表示這個這個表的外鍵是什么.注意,這里是表的外鍵,不是類的外鍵-->
</class>
</hibernate-mapping>
3>:<one-to-many>單身關聯
一對多關聯分為單向一對多關聯和雙向一雙多關聯
單向的一對多關聯只需要在一方進行映射配置
單身一對多關聯:
<hibernate-mapping>
<class table="customers" catalog="study">
<id type="java.lang.Long">
<column />
<generator />
</id>
<property type="java.lang.String">
<column length="20" />
</property>
<set table="orders" cascade="save-update" inverse="true">
<key column="customers_ID"></key>
<one-to-many />
</set>
</class>
</hibernate-mapping>
這里需要強調的是<set>元素的屬性代表的意義:
name
table
lazy:當為true時為延遲加載,為false時為立即加載
inverse:用于表示雙向關聯中的被動一端,inverse的值為false的一方負責維護關聯關系
cascade
sort:排序關系,unsorted(不排序),natural(自然排序),comparatorClass(由某個實現了java.util.comparator接口的類型 指定排序算法。);
******<key>子元素的column屬性指定關聯表(orders表)的外鍵(customers_ID)
4>:<one-to-many>雙向關聯:
如果要設置一對多雙向關聯關系.那么還需要在"多"的映射文件中使用<many-to-one>
<many-to-one
column="customers_ID"
cascade="none"
outer-join="auto"
insert="false" insert和update設定是否對column屬性指定的關聯字段進行insert和update操作
update="false">
</many-to-one>
4>多對多關聯:
多對多關聯時要建一個連接表查詢
學生的映射文件
<hibernate-mapping>
<class table="students" catalog="study">
<id type="java.lang.Long">
<column />
<generator />
</id>
<property type="java.lang.String">
<column length="20" />
</property>
<set table="student_teacher_table" cascade="save-update" inverse="false">
<key column="stuID"></key>
<many-to-many column="teaID"></many-to-many>
</set>
***對<key>元素的理解:
它的控制主要是通過stuID(外鍵)來完成,就是我們從student_teacher_table表中我們只要
select * from student_teacher_table where stuID='該學生的ID',這樣我們就可以得到它的教師的ID了
***對<many-to-many>的理解:
我們從student_teacher_table表中根據stuID拿到了與該stuID關聯的teaID,
然后select * from teacher where teaID='前一步拿到的teaID'
</class>
</hibernate-mapping>
教師的映射文件
<hibernate-mapping>
<class table="teachers" catalog="study">
<id type="java.lang.Long">
<column />
<generator />
</id>
<property type="java.lang.String">
<column length="20" />
</property>
<set table="student_teacher_table" inverse="true">
<key column="teaID"></key>
<many-to-many column="stuID"></many-to-many>
</set>
</class>
</hibernate-mapping>