在复合键hibernate xml中映射复合键

时间:2011-08-15 20:06:39

标签: java hibernate hibernate-mapping

这就是我想要做的,将对象映射到具有相同主键的另一个表。下面是一个例子,基本上我有一个具有复合键的对象,该复合键具有ANOTHER表的复合键,但我不知道如何包含两者以便创建正确的对象键。我突出显示了错误的行,它只包含密钥的一个属性。

  <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 个答案:

答案 0 :(得分:5)

仅包含复合外键businessRule的一个属性,因为<key-many-to-one仅声明一列BUSINESS_RULE。它应该在您的示例中声明它引用BUSINESS_RULEID_CD的复合键的两列。通过将ID_CD列添加到<key-many-to-one元素,您需要删除或重命名<key-property name="sameIdCode" column="ID_CD" />元素的列。

关联<key-many-to-onebusinessRule对象应该像这样映射:

<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参考文档
  5.1.7. composite-id
  Components as composite identifiers