2013-07-23 75 views
0

我有兩個父表和一個子表,複合鍵的下面提及:Hibernate的一對一的映射,有一個孩子和兩個父表

Employee: 
     employeeId1 (PK) 
     employeeId2 (PK) 
     employeeName 
     ---some other columns go here---- 


Student: 
     studentId1 (PK) 
     studentId2 (PK) 
     studentName 
     ---some other columns go here---- 

Address: 
     addressId (PK) 
     addressPersonId1 (PK & FK) 
     addressPersonId2 (PK & FK) 
     ---some other columns go here---- 

員工和學生可以有相同的地址,這樣員工和學生可以參考地址中的相同記錄。

這些都不是實際的表,我有我的應用程序,但結構保持不變,我不能不惜任何代價改變表結構。

我想實現(員工,地址)和(學生,地址)之間的一對一關係。 由於PK列的數量和PK列的名稱不同,我無法創建關係。

將Address(addressPersonId1,addressPersonId2)中的列指定爲Primary和Forign鍵的方法是什麼?以及如何從Parent hbm文件中引用這些forign鍵。

**I have to establish mapping using HBM files but not hibernate or JPA annotations.** 


**Is there any way to specify forign key column names in One to One tag in hbm file.** 

在此先感謝您的任何建議和解決方案。

Employee.hbm.xml 
<?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.model.Employee" table="EMPLOYEE"> 
     <composite-id name="employeePK" class="com.model.EmployeePK"> 
      <key-property name="employeeId" column="EMPLOYEE_ID" /> 
      <key-property name="employeeName" column="EMPLOYEE_NAME" /> 
     </composite-id> 

     <property name="employeeStatus" type="string"> 
      <column name="EMPLOYEE_STATUS" /> 
     </property> 

     <one-to-one name="address" class="com.model.Address" 
      cascade="all"> 
     </one-to-one> 

    </class> 
</hibernate-mapping> 


Student.hbm.xml 
<?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.model.Student" table="STUDENT"> 
     <composite-id name="studentPK" class="com.model.StudentPK"> 
      <key-property name="studentId" column="STUDENT_ID" /> 
      <key-property name="studentName" column="STUDENT_NAME" /> 
     </composite-id> 

     <property name="studentStatus" type="string"> 
      <column name="STUDENT_STATUS" /> 
     </property> 
    </class> 
</hibernate-mapping> 



Address.hbm.xml 
<?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.model.Address" table="ADDRESS"> 
     <composite-id name="addressPK" class="com.model.AddressPK"> 
      <key-property name="addressId" column="ADDRESS_ID" /> 
      <key-property name="addressPersonId" column="ADDRESS_PERSON_ID" /> 
      <key-property name="addressPersonName" column="ADDRESS_PERSON_NAME" /> 
     </composite-id> 

     <property name="addressStatus" type="string"> 
      <column name="ADDRESS_STATUS" /> 
     </property> 
    </class> 
</hibernate-mapping> 
+0

對於一對多的情況,我們可以在父hbm文件中指定映射列名爲** **有沒有什麼辦法可以在One to One關係中指定它。 –

+0

' ''是你的外鍵在其他表 – Angga

+0

讓我試試看...... –

回答

0

試試你一到一個

<one-to-one name="address" class="com.model.Address" cascade="all"></one-to-one> 

成爲前添加鍵值:

<key> 
    <column name="addressPK.studentCompositeId<!--<<im not sure about this, maybe you can try studentCompositeId-->" not-null="true" /> 
</key> 
<one-to-one name="address" class="com.model.Address" cascade="all"/> 

,並給予更改地址的2複合鍵變成1個複合(您需要以編輯類),以便在com.model.AddressPK中包含鍵addressId和com.model.StudentPK可能是這樣的:

<composite-id name="addressPK" class="com.model.AddressPK"> 
    <key-property name="addressId" column="ADDRESS_ID" /> 
    <composite-id name="studentCompositeId" class="com.model.StudentPK"> 
     <key-property name="studentId" type="string"> 
      <column name="ADDRESS_PERSON_ID"/> 
     </key-property> 
     <key-property name="studentName" type="string"> 
      <column name="ADDRESS_PERSON_NAME"/> 
     </key-property> 
    </composite-id> 
</composite-id> 
+0

好吧,我會試試這個... –