2013-04-28 90 views
2

我有兩個具有一對一關係的複合ID的表。表一:PK列:column1,column2 表二:PK列:column3,column4。當關鍵名稱不同時休眠一對一關聯

列有不同的名稱。我必須將column1映射到column3,將column2映射到column4。 表1(父表)聲明:

<class name="com.java.TABLE1" table="TABLE1"> 
     <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK"> 
      <key-property name="COLUMN1" column="COLUMN1" /> 
      <key-property name="COLUMN2" column="COLUMN3" /> 
     </composite-id> 
...remaining properties declarations... 

<one-to-one name="TABLE2" class="com.tc4.dal.TABLE2" 
      cascade="save-update, delete"> 
      <key column="COLUMN3" property-ref="COLUMN1" /> 
      <key column="COLUMN4" property-ref="COLUMN2" /> 
</one-to-one> 
</class> 

表2(子表)映射

<class name="com.java.TABLE2" table="TABLE2"> 
     <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK"> 
      <key-property name="COLUMN3" column="COLUMN3" /> 
      <key-property name="COLUMN4" column="COLUMN4" /> 
     </composite-id> 
</class> 

,同時啓動應用程序,獲得以下異常

Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/JAVA/TABLE1.hbm.xml 
    Caused by: org.xml.sax.SAXParseException: The content of element type "one-to-one" must match "(meta*,formula*)". 
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:196) 
    at org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:132) 
    at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:390) 

如何給予一對一的關聯在這種情況下映射文件。

Regards, Naresh。

+0

你可以向其他映射詳細資料,如你是如何映射的複合ID?與 2013-04-28 08:23:54

+0

根據問題'LNBCPRODHDRP0'具有鍵列名稱'BC_BANK_ID,BC_BRANCH_ID,BC_PRODUCT_NBR',但在您的情況下'LNBCPRODHDRP0_pk'指的是'LC_BANK_ID,LC_BRANCH_ID,LC_PRODUCT_NBR'也請編輯原始問題以添加此信息。不要在評論中提供代碼。 – 2013-04-28 08:59:26

+0

@ rahul maindargi,我編輯了我的問題。你能否檢查 – 2013-04-29 04:58:24

回答

1

將它們在父表中的子表複合鍵列重命名已解決此問題。 詳細闡釋: - - 父表(TABLE1):

<class name="com.java.TABLE1" table="TABLE1"> 
      <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK"> 
       <key-property name="COLUMN1Prop" column="COLUMN1" /> 
       <key-property name="COLUMN2Prop" column="COLUMN3" /> 
    </composite-id> 
    .... remaining properties 
    </class> 

//作爲TABLE1是具有組合大關鍵,需要爲TABLE1創建主鍵POJO

public class TABLE1_PK { 
    public String COLUMN1Prop; 
    public String COLUMN2Prop; 
    } 

TABLE1實體:

public class TABLE1{ 
    // declare PK 
    private TABLE1_PK TABLE1_pk; 
// getters and setters and remaining properties 
} 
  • 子表(表2):

    <class name="com.java.TABLE2" table="TABLE2"> 
            <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK"> 
             <key-property name="COLUMN1Prop" column="COLUMN3" /> 
             <key-property name="COLUMN2Prop" column="COLUMN4" /> 
          </composite-id> 
          .... remaining properties 
         </class> 
    

子實體PK

public class TABLE2_PK{ 
// here property names should be defined as parent Entity properties 
     public String COLUMN1Prop; 
     public String COLUMN2Prop; 
.. getters and setters 
} 
1

與註釋U可以這樣實現它。在表1

@OneToOne(targetEntity = TABLE2.class, fetch = FetchType.LAZY) 
    @JoinColumns(value = { 
     @JoinColumn(name = "COLUMN1", referencedColumnName = "COLUMN3"), 
     @JoinColumn(name = "COLUMN2", referencedColumnName = "COLUMN4") }) 
    TABLE2 table2 

使用XML試試這個,讓我知道

<class name="com.java.TABLE1" table="TABLE1"> 
     <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK"> 
      <key-property name="COLUMN1PROP" column="COLUMN1" /> 
      <key-property name="COLUMN2PROP" column="COLUMN3" /> 
     </composite-id> 
...remaining properties declarations... 

<one-to-one name="TABLE2" class="com.tc4.dal.TABLE2" 
      cascade="save-update, delete"> 
      <key column="COLUMN1PROP" property-ref="COLUMN3PROP" /> 
      <key column="COLUMN2PROP" property-ref="COLUMN4PROP" /> 
</one-to-one> 
</class> 

<class name="com.java.TABLE2" table="TABLE2"> 
     <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK"> 
      <key-property name="COLUMN3PROP" column="COLUMN3" /> 
      <key-property name="COLUMN4PROP" column="COLUMN4" /> 
     </composite-id> 
</class> 

或者

<class name="com.java.TABLE1" table="TABLE1"> 
     <composite-id name="TABLE1_pk" class="com.tc4.dal.TABLE1_PK"> 
      <key-property name="COLUMN1PROP" column="COLUMN1" /> 
      <key-property name="COLUMN2PROP" column="COLUMN3" /> 
     </composite-id> 
...remaining properties declarations... 

<one-to-one name="TABLE2" class="com.tc4.dal.TABLE2" 
      cascade="save-update, delete"> 
      <key column="COLUMN1" property-ref="COLUMN3PROP" /> 
      <key column="COLUMN2" property-ref="COLUMN4PROP" /> 
</one-to-one> 
</class> 

<class name="com.java.TABLE2" table="TABLE2"> 
     <composite-id name="TABLE2_pk" class="com.tc4.dal.TABLE2_PK"> 
      <key-property name="COLUMN3PROP" column="COLUMN3" /> 
      <key-property name="COLUMN4PROP" column="COLUMN4" /> 
     </composite-id> 
</class> 

讓我knwo哪一個工作。 :)

+1

Hi Rahul,謝謝你的回覆。我正在使用基於xml的映射。我已經嘗試過你的第二種情況,這仍然會導致異常。我懷疑你的第一個案件是否會工作,因爲,這裏列標籤應該是指一列(<鍵列=「COLUMN1PROP」屬性-REF =「COLUMN3PROP」 />) – 2013-04-29 14:05:31

+0

@拉胡爾邁恩達爾吉,有任何意見或任何其他建議? – 2013-05-03 08:43:01

+0

以及我也在搜索並通過hibernate文檔。我找到的東西:) – 2013-05-04 12:49:06