Memran
Memran

Reputation: 405

Hibernate one-to-one with composite keys

I have an issue with my mapping file, relating to a one-to-one relationship, with composite primary keys, where the field names of the keys do not match.

Table 1:

<class entity-name="CompPkTest" table="compPkTest" catalog="data" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
<composite-id mapped="false" unsaved-value="undefined">
  <key-property name="id1" type="int">
    <column name="id1"/>
  </key-property>
  <key-property name="id2" type="int">
    <column name="id2"/>
  </key-property>
</composite-id>
<property name="details" type="string" unique="false" optimistic-lock="true" lazy="false" generated="never">
  <column name="Details" length="500"/>
</property>
<one-to-one name="CompPkTestDetail" entity-name="CompPkTestDetail" constrained="false" embed-xml="true"/>

Table 2:

<class entity-name="CompPkTestDetail" table="compPkTestDetail" catalog="data" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
<composite-id mapped="false" unsaved-value="undefined">
  <key-property name="idetail1" type="int">
    <column name="idetail1"/>
  </key-property>
  <key-property name="idetail2" type="int">
    <column name="idetail2"/>
  </key-property>
</composite-id>
<one-to-one name="CompPkTest" entity-name="CompPkTest" constrained="true" embed-xml="true"/>
<property name="someDetail" type="string" unique="false" optimistic-lock="true" lazy="false" generated="never">
  <column name="someDetail" length="300"/>
</property>
<property name="moreDetail" type="string" unique="false" optimistic-lock="true" lazy="false" generated="never">
  <column name="moreDetail" length="300"/>
</property>

The problem is that when querying table 1, the "details" property is null. If I change the key-property names in CompPkTestDetail to be id1 and id2, (while leaving the column name unchanged), the relationship works as expected, and the query returns a value for "details".

My questions:

Is the above mapping xml the correct way to make this association (with the non-matching field names)?

Is hibernate correct to impose that the field names must match, or is this a bug?

Please note that in my application there are no classes for the entities, and hibernate is in map-mode. My application has fully dynamic access to the (arbitrary) databases, and so the xml for the mappings is generated at run time.

Upvotes: 0

Views: 4172

Answers (1)

Firo
Firo

Reputation: 30803

<class entity-name="CompPkTest" table="compPkTest" >
  <composite-id>
    <key-property name="id1" column="id1"/>
    <key-property name="id2" column="id2"/>
  </composite-id>
  <one-to-one name="CompPkTestDetail" entity-name="CompPkTestDetail" property-ref="Parent"/>
</class>

<class entity-name="CompPkTestDetail" table="compPkTestDetail">
  <composite-id>
    <key-many-to-one name="Parent" entity-name="CompPkTest" >
      <column name="idetail1"/>
      <column name="idetail2"/>
    </key-many-to-one>
  </composite-id>
  <!--maybe needed if property-ref does not find the id property-->
  <property name="Parent" insert="false" update="false">
    <column name="idetail1"/>
    <column name="idetail2"/>
  </property>
</class>

the Detail should have a normal reference to its parent and the Master references it. I'm not sure if you have to specify property-ref in the one-to-one to the Parent property

Upvotes: 1

Related Questions