2013-02-28 185 views
0

我是一個新的bie的hibernate的世界,你可以請指教,我正在通過Hibernate的一對一關係, 根據我的分析,可以建立一對一的hibernate關係通過三種方式..關於一對一映射休眠

1)Through Join concept 
2)Same primary key in both the tables 
3)Primary key and foriegn key relationship in both the tables 

請指教,以上三種方式來實現一對一的映射是正確的還是我失去了一些東西,然後請指教,也請提醒,以下HBM映射文件,我我使用的是正確的one.if然後請告知

1)通過加入概念: -
一種方式來實現,以一對一的關係是通過接合的概念,以下被用於在兩個表的是

<hibernate-mapping> 

<class name="mypack.Person" table="person21"> 
<id name="personId" type="int"> 
<generator class="increment"> 
</generator> 
</id> 
<property name="name"/> 

<join table="personAddress"> 
<key column="personId"/> 
<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/> 
</join> 
</class> 

<class name="mypack.Address" table="address21"> 
<id name="id" column="addressId" type="int"> 
<generator class="increment"/> 
</id> 
<property name="city"/> 
<property name="state"/> 
</class> 
</hibernate-mapping> 

2)相同的主鍵的xml : -

使用在這兩個表中相同的主鍵,以下HBM被用於該

<class name="mypack.Address" table="address31"> 
<id name="id" column="addressId" type="int"> 
<generator class="increment"/> 
</id> 
<property name="city"/> 
<property name="state"/> 
</class> 

<class name="mypack.Person" table="person31"> 
<id name="personId" type="int"> 
<generator class="foreign"> 
<param name="property">address</param> 
</generator> 
</id> 
<property name="name"/> 
<one-to-one name="address" class="mypack.Address"/> 
</class> 
</hibernate-mapping> 

3)普瑞瑪ry鍵和foriegn在兩個表中的關鍵關係: -

主鍵在一個表中,foriegn鍵在另一個表中。下面是這個hbm

<hibernate-mapping> 
<class name="mypack.Person"> 
<id name="personId" type="int"> 
<generator class="increment"/> 
</id> 
<property name="name"/> 

<many-to-one name="address" class="mypack.Address" column="addressId" unique="true" cascade="all"/> 
</class> 


<class name="mypack.Address"> 
<id name="id" column="addressId" type="int"> 
<generator class="increment"/> 
</id> 
<property name="city"/> 
<property name="state"/> 
</class> 
</hibernate-mapping> 

夥計們請指教。

+0

你有沒有考慮使用註釋,而不是XML映射?如果你是新手,可能會更容易,並且有很多好的教程可以開始。 – Magnilex 2013-02-28 17:37:36

+0

@MagnusTengdahl謝謝,是的,我必須選擇XML,請告知這個hbm xml映射是正確的還是他們需要任何修改 – user2094103 2013-03-01 02:23:28

+0

我不使用xml映射和休眠,我有強烈的偏好使用註釋。但是您正嘗試執行簡單的一對一關係,只需按照以下教程進行操作即可:http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to- one-1.html – Magnilex 2013-03-01 07:35:35

回答

0

我真的認爲連接表在這裏是過度殺傷的一對一映射。對於其他兩種解決方案,請看下面的Hibernate文檔,所有內容都被解釋爲:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-onetoone

對於您的第二個映射(主鍵關聯),兩個實體都需要定義一對一映射,並且關係的一側還需要指定屬性constrained="true"

這是我會怎麼寫你的第二個映射:

<class name="mypack.Person" table="PERSONS"> 
    <id name="id" type="int" column="PERSON_ID"> 
     <generator class="increment"/> 
    </id> 
    <property name="name" type="string" column="NAME"/> 
    <one-to-one name="address" class="mypack.Address" cascade="all"/> 
</class> 

<class name="mypack.Address" table="ADDRESSES"> 
    <id name="id" type="int" column="ADDRESS_ID"> 
     <generator class="foreign"> 
      <param name="property">address</param> 
     </generator> 
    </id> 
    <property name="city" type="string" column="CITY"/> 
    <property name="state" type="string" column="STATE"/> 
    <one-to-one name="person" class="mypack.Person" constrained="true"/> 
</class> 
+0

非常感謝,關於第二映射,請讓我知道有一個網址http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/ ..它有顯示你想要溝通的方式 – user2094103 2013-03-01 13:26:19

+0

我用一個映射例子編輯了我的答案 – overmeulen 2013-03-01 13:40:10

+0

非常感謝你也請爲第三個映射提供建議。 – user2094103 2013-03-02 04:04:50