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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7070150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:31:44  来源:igfitidea点击:

Map a composite key inside a composite key hibernate xml

javahibernatehibernate-mapping

提问by Organiccat

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.

这就是我想要做的,将一个对象映射到另一个具有相同主键的表。下面是一个示例,基本上我有一个带有复合键的对象,该对象具有 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>

回答by Joel Hudon

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

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

The association <key-many-to-oneto businessRuleobject should be mapped like this:

该协会<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 reference documenation
5.1.7. composite-id
Components as composite identifiers

Hibernate 参考文档
5.1.7。Composite-id
组件作为复合标识符