Map a composite key inside a composite key hibernate xml

▼魔方 西西 提交于 2019-12-06 11:56:46

问题


This is what I would like to do, map an object to another table that has the same primary keys. The below is an example, basically I have one object with a composite key that has a composite key for ANOTHER table, but I don't know how to include both in order to create the proper object key. I highlighted the row that is wrong, it only includes one of the properties for the key.

  <class name="BusinessRuleObject" table="BUSINESS_RULE_OBJECTS" schema="DB">
    <composite-id name="businessRuleObjectId" class="BusinessRuleObjectId">
      <key-property name="sameIdCode" column="ID_CD" />
      **<key-many-to-one name="businessRule" class="BusinessRule" column="BUSINESS_RULE" />**
    </composite-id>
    <!-- ... STUFF GOES HERE -->
  </class>

  <class name="BusinessRule" table="BUSINESS_RULE_STRINGS" schema="DB">
    <composite-id name="businessRule2ID" class="BusinessRule2ID">
      <key-property name="sameIdCode" column="ID_CD" />
      <key-property name="businessRuleCode" column="BUSINESS_RULE" />
    </composite-id>
    <!-- TOTALLY DIFFERENT STUFF GOES HERE -->
   </class>

回答1:


Only one properties of the composite foreign key businessRule is included, because the <key-many-to-one only declare one column BUSINESS_RULE. It should declare the two column of the composite key it is referencing BUSINESS_RULE and ID_CD in your example. By adding the ID_CD column to the <key-many-to-one element you need to remove or rename the column of <key-property name="sameIdCode" column="ID_CD" /> element.

The association <key-many-to-one to businessRule object should be mapped like this:

<composite-id name="businessRuleObjectId" class="BusinessRuleObjectId"> 

  <key-many-to-one name="businessRule" class="BusinessRule" >
     <column name="ID_CD" />
     <column name="BUSINESS_RULE" />
  </key-many-to-one>

</composite-id> 

Hibernate reference documenation
5.1.7. composite-id
Components as composite identifiers



来源:https://stackoverflow.com/questions/7070150/map-a-composite-key-inside-a-composite-key-hibernate-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!