-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: many-to-many with composite id
PostPosted: Mon Mar 22, 2004 4:09 pm 
Beginner
Beginner

Joined: Tue Feb 03, 2004 10:12 pm
Posts: 20
Location: San Francisco
Hi-
I need some help on a many-to-many mapping. I have:
Table A:
A1, B1, C1 (composite primary key)
Table B:
B2, C2, D2 (composite primary key)

B1 = B2, C1 = C2. There is a many-to-many relationship between Class1 (table A) and Class2 (Table B).

I have tried the following:
Code:
<class name="Class1" table="TableA">
    <composite-id name="key"  class="Class1PK">
        <key-property name="keyA1" type="long" column="A1"/>
        <key-property name="KeyB1" type="long" column="B1"/>
        <key-property name="KeyC1" type="long" column="C1"/>
   </composite-id>
   ....
   <set name="members" table="TableB" lazy="true">
    <key>
        <column name="B1"/>
        <column name="C1"/>
    </key>
    <many-to-many class="Class2" outer-join="false">
        <column name="D2"/>
     </many-to-many>
  </set>
</class>

and:
<class name="Class2" table="TableB" mutable="false">
   <composite-id name="key" class="Class2PK">
      <key-property name="keyB2" type="long" column="B2"/>
      <key-property name="keyC2" type="long" column="C2"/>
      <key-property name="keyD2" type="long" column="D2"/>
    </composite-id>
    ....
</class>


And I end up with this exception:
net.sf.hibernate.MappingException: Foreign key (TableB [D2])) must have same number of columns as the referenced primary key (TableB [A1,B1,D2]])

If you know the solution you can probably see that I can't figure out which column goes where. I tried other combinations without success. I would appreciate any help on the subject.
Thanks

_________________
Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 4:47 pm 
Beginner
Beginner

Joined: Tue Feb 03, 2004 10:12 pm
Posts: 20
Location: San Francisco
Note: I also tried this combination which seemed more correct:
(B1 and C1 are the FK reference to Table B from Table A and
B2, C2 are the FK reference to Table A from Table B):
Code:
<class name="Class1" table="TableA">
    <composite-id name="key"  class="Class1PK">
        <key-property name="keyA1" type="long" column="A1"/>
        <key-property name="KeyB1" type="long" column="B1"/>
        <key-property name="KeyC1" type="long" column="C1"/>
   </composite-id>
   ....
   <set name="members" table="TableB" lazy="true">
    <key>
        <column name="B1"/>
        <column name="C1"/>
    </key>
    <many-to-many class="Class2" outer-join="false">
        <column name="B2"/>
        <column name="C2"/>
     </many-to-many>
  </set>
</class>


With this error:
net.sf.hibernate.MappingException: Foreign key (TableB [B2,C2])) must have same number of columns as the referenced primary key (TableA [A1,B1,C1])

_________________
Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 8:30 pm 
Beginner
Beginner

Joined: Tue Feb 03, 2004 10:12 pm
Posts: 20
Location: San Francisco
I have overlooked the fact that a third table is required by Hibernate. I am not sure I understand why it is the case. The above pattern doesn't seem incorrect to me and being able to link two classes/tables like that should be common.

_________________
Nicolas


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.