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