Joining Tables with Composite Primary Keys in Hibernate hbm files

1.5k views Asked by At

I have two table to be joined using hbm file.

The scenario is as follows

Table B has a composite PK.

Table B

a1 (pk) a2 (pk) a3 (pk) foo bar

========

Table A has a primary key and is a foreign key from Table B

Table A

a1 (pk,fk) hip hop

========

This a one to many relation from Table A to B.

Can any one help me out with the hbm file mapping?

I am jotting down wot i have tried.

For Table B

<class>
<composite-id name="XXXX" class=".....">
        <key-property name="a1" column="a1" type="int"/>
        <key-property name="a2" column="a2" type="int"/>
        <key-property name="a3" column="a3" type="int"/>
    </composite-id>

    <property name="foo" column="foo" type="java.lang.Float"></property>    
    <property name="bar" column="bar" type="java.lang.Float"></property>
    </class>

For Table A

Normal hibernate mapping.

How to join both tables?

1

There are 1 answers

0
Firo On BEST ANSWER

a workaround could be afake propertyref to break out of the compositeId

<class class="B">
  <property name="a1_again" column="a1" insert="false" update="false"/>

  <bag name="As" class="A">
    <key property-ref="a1_again"></key>
    ...
  </bag>
</class>