问题
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