-->
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.  [ 1 post ] 
Author Message
 Post subject: Help with imbedded one-to-many associations
PostPosted: Tue Aug 23, 2005 10:39 am 
Newbie

Joined: Tue Aug 23, 2005 10:02 am
Posts: 7
This is driving me crazy. I have three tables: User, Tab, Tile. I also have three associated classes with the same names. The primary key of User is userID, the primary key of Tab is userID and tabName, the primary key of Tile is userID, tabName, and tileName. Tab has a foreign key of userID pointing to the parent User record, and Tile has a foreign key of userID and tabName pointing to the parent Tab record. A User object contains a collection of Tab objects, which in turn contain a collection of Tile objects. Since a Tab is contained in a User object, the Tab class doesn't have a userID attribute (since that's part of the containing user object). Likewise, since a Tile is contained in a Tab object which is contained in a User object, the Tile class doesn't have a userID attribute or a tabName attribute. Without Hibernate, this would be simple to implement. However, I'm having troubles doing this in Hibernate. Here's my config XML for the classes:

<class name="com.ibm.caes.wcs.db.cache.beans.User" table="user">
<id name="userId"/>
<bag name="tabs" lazy="false" cascade="all">
<key column="userid" not-null="true"/>
<one-to-many class="com.ibm.caes.wcs.db.cache.beans.Tab"/>
</bag>
</class>

<class name="com.ibm.caes.wcs.db.cache.beans.Tab" table="tab">
<id name="name" column="tabname"/>
<bag name="tiles" lazy="false" cascade="all">
<key>
<column name="userid" not-null="true"/>
<column name="tabname" not-null="true"/>
</key>
<one-to-many class="com.ibm.caes.wcs.db.cache.beans.Tile"/>
</bag>
</class>

<class name="com.ibm.caes.wcs.db.cache.beans.Tile" table="tile">
<id name="name" column="tilename"/>
</class>

So you see, my User class has a collection (bag) of tab objects (defined as a simple one-to-many parent/child association) using userId as the key into the Tab table. The Tab class has a collection (bag) of tile objects (again defined as a simple one-to-may parent/child association) using the userid and tabname as the keys into the Tile table. But because I don't have the userId defined in the Tab class as part of the class's ID, I get the dreaded "Foreign key must have same number of columns as the referenced primary key" error. Aside from adding the userID attribute to my Tab class (which I really don't want to do) is there a way to handle this problem? Thanks.


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.