这就是我想要做的,将对象映射到具有相同主键的另一个表。下面是一个例子,基本上我有一个具有复合键的对象,该复合键具有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>
答案 0 :(得分:5)
仅包含复合外键businessRule
的一个属性,因为<key-many-to-one
仅声明一列BUSINESS_RULE
。它应该在您的示例中声明它引用BUSINESS_RULE
和ID_CD
的复合键的两列。通过将ID_CD
列添加到<key-many-to-one
元素,您需要删除或重命名<key-property name="sameIdCode" column="ID_CD" />
元素的列。
关联<key-many-to-one
到businessRule
对象应该像这样映射:
<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