-->
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: Bi-Directional many-to-many with composite keys
PostPosted: Mon Jan 21, 2008 6:11 pm 
Newbie

Joined: Wed Jan 09, 2008 11:30 pm
Posts: 9
I just cant seem to come up with a valid mapping.
Code:
<hibernate-mapping>
   <class name="mypackage.Tokens" table="TOKENS"
      schema="PUBLIC">
      <composite-id name="id"
         class="mypackage.TokensId">
         <key-property name="appid" type="string">
            <column name="APPID" length="34" />
         </key-property>
         <key-property name="token" type="string">
            <column name="TOKEN" length="128" />
         </key-property>
      </composite-id>
      <set name="belongsTo" table="TokenGroups">
         <key column="Tokens"/>
         <many-to-many column="GroupsId" class="mypackage.Groups"/>
      </set>
   </class>
</hibernate-mapping>

Code:
<hibernate-mapping>
   <class name="mypackage.Groups" table="GROUPS"
      schema="PUBLIC">
      <composite-id name="id"
         class="mypackage.GroupsId">
         <key-property name="appid" type="string">
            <column name="APPID" length="34" />
         </key-property>
         <key-property name="groupname" type="string">
            <column name="GROUPNAME" length="128" />
         </key-property>
      </composite-id>
      <set name="members" inverse="true" table="UserGroups">
         <key column="GroupID"/>
         <many-to-many column="UserID" class="mypackage.Users"/>
      </set>
      <set name="tokenSet" inverse="true" table="TokenGroups">
         <key column="Group"/>
         <many-to-many column="Token" class="mypackage.Tokens"/>
      </set>
   </class>
</hibernate-mapping>

this seems to be close, but I get a complaint about not having enough columns in the foreign key relationship

Code:
org.hibernate.MappingException: Foreign key (FKF5E4090D1085596A:TokenGroups [Token])) must have same number of columns as the referenced primary key (TOKENS [APPID,TOKEN])


I've been reading through the documentation, and googling, but I have yet to find something that points me n the right direction


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 6:13 pm 
Newbie

Joined: Wed Jan 09, 2008 11:30 pm
Posts: 9
I'd better actually ask the question;

Can somebody please tell me what I'm doing wrong, or which step I've missed?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 1:02 pm 
Beginner
Beginner

Joined: Mon Mar 07, 2005 6:23 pm
Posts: 21
Did you auto-generate your mapping files from Hibernate Tools? If I remember correctly, it has a problem accurately doing many-to-many mappings that use composite keys...

Regardless, you need to reference both pk/fk columns in your many-to-many setup - notice it's only referencing one of them on both sides.

something like this might work:

Code:
   
<set name="belongsTo" table="TokenGroups">
      <key>
        <column name="APPID"/>
        <column name="TOKEN"/>
      </key>
      <many-to-many class="mypackage.Groups">
        <column name="APPID"/>
        <column name="GROUPNAME"/>
      </many-to-many>
</set>


Are you sure you've got your keys set up right? both Groups and Tokens both have an "APPID" column, but your many-to-many mapping in Groups references a GroupID column...

Anyway, the basic idea is that your many-to-many table should include the PK(s) from the tables on both sides.


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.