歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hibernate映射文件set key one-to-many

Hibernate映射文件set key one-to-many

日期:2017/3/1 9:51:40   编辑:Linux編程

hibernate映射文件set key one-to-many的配置。
POJOs如下:
Customer類------>customer表 Order類對應---------->orders表 customer(1)<--------------->(n)order
publicclassCustomer
{
privateString id;
privateString username;
privateString password;
privateTimestamp registerTime;
privateintage;
privateSet<Order> orders = newHashSet<Order>();

/*setter and getter method*/
}
publicclassOrder
{
privateString id;
privateString orderNumber;
privateintbalance;
privateCustomer customer;

/*setter and getter method*/

Set集合映射:
Hibernate為集合映射提供了專用的標簽元素,Set集合映射,就使用<set>標簽表示:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.suxiaolei.hibernate.pojos.Customer"table="customer">
<!--主鍵設置 -->
<id name="id"type="string">
<column name="id"></column>
<generator class="uuid"></generator>
</id>

<!--屬性設置 -->
<property name="username"column="username"type="string"></property>
<property name="password"column="password"type="string"></property>
<property name="age"column="age"type="integer"></property>
<property name="registerTime"column="register_time"type="timestamp"></property>

<set name="orders"inverse="true"cascade="all">
<key column="customer_id"></key>
<one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>
</set>

</class>
</hibernate-mapping>

<set>標簽中的"name"屬性表示customer對象中關系集合的屬性名,"inverse"與"cascade"屬性說明(參考這裡)。在數據庫中表示"一對多"的關系是通過外鍵關聯的方式實現的,"多方"通過持有"一方"的主鍵值來確定關系,怎麼持有"一方"的主鍵值?"多方"將使用一列來存儲"一方"的主鍵值,然後將此列作為外鍵列參照"一方"的主鍵列。所以使用Hibernate開發時需要將兩表的關系列(外鍵列)告訴Hibernate,<key column="customer_id"></key>就是完成這個工作的,Hibernate就能根據 "customer_id"列取出關聯信息。例如:從customer表中取出一條記錄後,Hibernate會根據該customer記錄的主鍵值再從order表中查找"custom_id"列,取出值相等的記錄,然後組裝到Customer對象中的set集合屬性中,反之亦然。因為取出來的記錄(只是一些零碎的值,還沒有組裝成對象)需要存放到Set集合中,所以要告訴Hibernate在Set集合裡面能放什麼類型的數據。<one-to-many>這個標簽就是完成這個工作的,"class"屬性是指定這個這個Set集合裡面元素的類型。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.suxiaolei.hibernate.pojos.Order"table="orders">
<id name="id"type="string">
<column name="id"></column>
<generator class="uuid"></generator>
</id>

<property name="orderNumber"column="orderNumber"type="string"></property>
<property name="balance"column="balance"type="integer"></property>

<many-to-one name="customer"class="com.suxiaolei.hibernate.pojos.Customer">
<column name="customer_id"></column>
</many-to-one>
</class>
</hibernate-mapping>
<many-to-one>標簽是設置"一對多"關系中的"多方"的,name指定了哪一個屬性是關系屬性,"class"指定了關系屬性的類型(也指定了與哪一個表關聯), "column"屬性是指定這個關聯屬性是按照"customer_id"列的值,在customer表中查詢獲得的。

Hibernate 的詳細介紹:請點這裡
Hibernate 的下載地址:請點這裡

Hibernate 中文手冊 PDF http://www.linuxidc.com/Linux/2013-10/91208.htm

Copyright © Linux教程網 All Rights Reserved