0

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.

Memran
  • 405
  • 4
  • 12

1 Answers1

1
<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

Firo
  • 30,626
  • 4
  • 55
  • 94
  • Thanks for taking the time to reply, however the above method results in a GenericJDBCException when performing a query on CompPkTest, which, so far, I'm not able to resolve. – Memran Nov 05 '12 at 09:23
  • have you tried adding the property-ref? Where is the stacktrace? – Firo Nov 05 '12 at 09:24
  • Apologies! The stack trace was unrelated. I don't understand where to put the property-ref, because I thought it needs to be on the one/many-to-one tag of the 'child', but there isn't one. Anyway, now when querying, (entity-mode MAP) the map's value for 'detail' is null. (When the ids match it has an object here). Hope that makes sense! – Memran Nov 05 '12 at 10:10
  • Thanks again, but there's still something not right. `Caused by org.jboss.seam.InstantiationException with message: "Could not instantiate Seam component: test_1345107808962Factory" Caused by org.hibernate.InvalidMappingException with message: "Could not parse mapping document from resource test_1345107808962.dynamic.cfg.xml" Caused by org.hibernate.MappingException with message: "you must specify types for a dynamic entity: Parent"` But given that my databases are arbitrary, I'm not sure how I can specify a type. (Remember my XML is generated at run time, by examining the DB) – Memran Nov 05 '12 at 13:23
  • At this stage, I'm not sure I can spend too much more time on this, given that I have a (not very nice) work-around. There are too many other things to do too. I'll have to revisit this again later. – Memran Nov 05 '12 at 13:58