8.5.1. 一對多(one to many) /多對一( many to one)
基于連接表的雙向一對多關聯。注意inverse="true"可以出現在關聯的任意一端,即collection端或者join端。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses"
table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
inverse="true"
optional="true">
<key column="addressId"/>
<many-to-one name="person"
column="personId"
not-null="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null primary key )
create table Address ( addressId bigint not null primary key )
基于連接表的雙向一對一關聯極為罕見,但也是可行的。
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true">
<key column="personId"
unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"
unique="true"/>
</join>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true"
inverse="true">
<key column="addressId"
unique="true"/>
<many-to-one name="address"
column="personId"
not-null="true"
unique="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )
最后,還有 雙向多對多關聯.
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="people" inverse="true">
<key column="addressId"/>
<many-to-many column="personId"
class="Person"/>
</set>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )
Component這個概念在Hibernate中幾處不同的地方為了不同的目的被重復使用.
9.1. 依賴對象(Dependent objects)
Component是一個被包含的對象,它作為值類型被持久化,而非一個被引用的實體。“component(組件)”這一術語指的是面向對象的合成概念(而并不是系統構架層次上的組件的概念)舉個例子, 你可以對人(Person)如以下這樣來建模:
public class Person {
private java.util.Date birthday;
private Name name;
private String key;
public String getKey() {
return key;
}
private void setKey(String key) {
this.key=key;
}
public java.util.Date getBirthday() {
return birthday;
}
public void setBirthday(java.util.Date birthday) {
this.birthday = birthday;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
......
......
}
public class Name {
char initial;
String first;
String last;
public String getFirst() {
return first;
}
void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
void setLast(String last) {
this.last = last;
}
public char getInitial() {
return initial;
}
void setInitial(char initial) {
this.initial = initial;
}
}
現在,姓名(Name)是作為人(Person)的一個組成部分。需要注意的是:需要對姓名 的持久化屬性定義getter和setter方法,但是不需要實現任何的接口或申明標識符字段。
以下是這個例子的Hibernate映射文件:
<class name="eg.Person" table="person">
<id name="Key" column="pid" type="string">
<generator class="uuid.hex"/>
</id>
<property name="birthday" type="date"/>
<component name="Name" class="eg.Name"> <!-- class attribute optional -->
<property name="initial"/>
<property name="first"/>
<property name="last"/>
</component>
</class>
人員(Person)表中將包括pid, birthday, initial, first和 last等字段。
就像所有的值類型一樣, Component不支持共享引用。 換句話說,兩個人可能重名,但是兩個person對象應該包含兩個獨立的name對象,只不過是具有“同樣”的值。 Component的值為空從語義學上來講是專有的(ad hoc)。 每當 重新加載一個包含組件的對象,如果component的所有字段為空,那么將Hibernate將假定整個component為 空。對于絕大多數目的,這樣假定是沒有問題的。
Component的屬性可以是Hibernate類型(包括Collections, many-to-one 關聯, 以及其它Component 等等)。嵌套Component不應該作為特殊的應用被考慮(Nested components should not be considered an exotic usage)。 Hibernate趨向于支持設計細致(fine-grained)的對象模型。
<component> 元素還允許有 <parent>子元素 ,用來表明component類中的一個屬性返回包含它的實體的引用。
<class name="eg.Person" table="person">
<id name="Key" column="pid" type="string">
<generator class="uuid.hex"/>
</id>
<property name="birthday" type="date"/>
<component name="Name" class="eg.Name" unique="true">>
<parent name="namedPerson"/> <!-- reference back to the Person -->
<property name="initial"/>
<property name="first"/>
<property name="last"/>
</component>
</class>
Hibernate支持component的集合(例如: 一個元素是“姓名”這種類型的數組)。 你可以使用<composite-element>標簽替代<element>標簽來定義你的component集合。
<set name="someNames" table="some_names" lazy="true">
<key column="id"/>
<composite-element class="eg.Name"> <!-- class attribute required -->
<property name="initial"/>
<property name="first"/>
<property name="last"/>
</composite-element>
</set>
注意,如果你決定定義一個元素是聯合元素的Set,正確地實現equals()和hashCode()是非常重要的。
組合元素可以包含component但是不能包含集合。如果你的組合元素自身包含component, 必須使用<nested-composite-element>標簽。這是一個相當特殊的案例 - 組合元素的集合自身可以包含component。 這個時候你就應該考慮一下使用one-to-many關聯是否會更恰當。 嘗試對這個組合元素重新建模為一個實體-但是需要注意的是,雖然Java模型和重新建模前 是一樣的,關系模型和持久性語義上仍然存在輕微的區別。
請注意如果你使用<set>標簽,一個組合元素的映射不支持可能為空的屬性. 當刪除對象時, Hibernate必須使用每一個字段的來確定一條記錄(在組合元素表中,沒有單個的關鍵字段), 如果有為null的字段,這樣做就不可能了。你必須作出一個選擇,要么在組合元素中使用不能為空的屬性, 要么選擇使用<list>, <map>,<bag> 或者 <idbag>而不是 <set>。
組合元素有個特別的案例,是組合元素可以包含一個<many-to-one> 元素。類似這樣的映射允許你映射一個many-to-mang關聯表作為組合元素額外的字段。(A mapping like this allows you to map extra columns of a many-to-many association table to the composite element class.) 接下來的的例子是從Order到Item的一個多對多的關聯關系,而 purchaseDate, price 和 quantity 是Item的關聯屬性。
<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.Purchase">
<property name="purchaseDate"/>
<property name="price"/>
<property name="quantity"/>
<many-to-one name="item" class="eg.Item"/> <!-- class attribute is optional -->
</composite-element>
</set>
</class>
當然,在另一方面,無法存在指向purchase的關聯,因此不能實現雙向關聯查詢。記住組建是值類型,并且不允許共享關聯。單個Purchase 可以放在包含Order的集合中,但它不能同時被Item所關聯。
即使三重或多重管理都是可能的:
<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.OrderLine">
<many-to-one name="purchaseDetails" class="eg.Purchase"/>
<many-to-one name="item" class="eg.Item"/>
</composite-element>
</set>
</class>
在查詢中,組合元素使用的語法是和關聯到其他實體的語法一樣的。
9.3. 組件作為Map的索引(Components as Map indices )
<composite-map-key>元素允許你映射一個Component類作為Map的key, 但是你必須確定你正確的在這個類中重寫了hashCode() 和 equals()方法。
9.4. 組件作為聯合標識符(Components as composite identifiers)
你可以使用一個component作為一個實體類的標識符。 你的component類必須滿足以下要求:
注意:在Hibernate3中,第二種要求并非是Hibernate強制必須的。但最好這樣做。
你不能使用一個IdentifierGenerator產生組合關鍵字。作為替代應用程序必須分配它自己的標識符。
使用<composite-id> 標簽(并且內嵌<key-property>元素)代替通常的<id>標簽。 比如,OrderLine類具有一個依賴Order的(聯合)主鍵的主鍵。
<class name="OrderLine">
<composite-id name="id" class="OrderLineId">
<key-property name="lineId"/>
<key-property name="orderId"/>
<key-property name="customerId"/>
</composite-id>
<property name="name"/>
<many-to-one name="order" class="Order"
insert="false" update="false">
<column name="orderId"/>
<column name="customerId"/>
</many-to-one>
....
</class>
現在,任何關聯到OrderLine 的外鍵都是復合的。在你的映射文件中,必須為其他類也這樣聲明。指向OrderLine的關聯可能被這樣映射:
<many-to-one name="orderLine" class="OrderLine">
<!-- the "class" attribute is optional, as usual -->
<column name="lineId"/>
<column name="orderId"/>
<column name="customerId"/>
</many-to-one>
(注意在各個地方<column>標簽都是column屬性的替代寫法。)
指向OrderLine的多對多關聯也使用聯合外鍵:
<set name="undeliveredOrderLines">
<key column name="warehouseId"/>
<many-to-many class="OrderLine">
<column name="lineId"/>
<column name="orderId"/>
<column name="customerId"/>
</many-to-many>
</set>
在Order中, OrderLine的集合則是這樣:
<set name="orderLines" inverse="true">
<key>
<column name="orderId"/>
<column name="customerId"/>
</key>
<one-to-many class="OrderLine"/>
</set>
(與通常一樣,<one-to-many>元素不聲明任何列.)
假若OrderLine本身擁有一個集合,它也具有組合外鍵。
<class name="OrderLine">
....
....
<list name="deliveryAttempts">
<key> <!-- a collection inherits the composite key type -->
<column name="lineId"/>
<column name="orderId"/>
<column name="customerId"/>
</key>
<list-index column="attemptId" base="1"/>
<composite-element class="DeliveryAttempt">
...
</composite-element>
</set>
</class>
9.5. 動態組件 (Dynamic components)
你甚至可以映射Map類型的屬性:
<dynamic-component name="userAttributes">
<property name="foo" column="FOO"/>
<property name="bar" column="BAR"/>
<many-to-one name="baz" class="Baz" column="BAZ_ID"/>
</dynamic-component>
從<dynamic-component>映射的語義上來講,它和<component>是相同的。 這種映射類型的優點在于通過修改映射文件,就可以具有在部署時檢測真實屬性的能力.利用一個DOM解析器,是有可能在運行時刻操作映射文件的。 更好的是,你可以通過Configuration對象來訪問(或者修改)Hibernate的運行時元模型。