-->
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: Foreign Key Relationship with different names for same id
PostPosted: Thu Oct 02, 2003 5:46 pm 
Newbie

Joined: Thu Oct 02, 2003 5:39 pm
Posts: 4
Hi There,

I've searched the forum but can not find an answer to the relationship's I want to map.
I have two tables CATEGORY and CATEGORY_GROUP. For simplicity sakes CATEGORY contains a single ID called CAT_ID and CATEGORY_GROUP contains a composite key called PARENT_CAT_ID and CHILD_CAT_ID. This represents a recursive relationship where each category can have many child categories and many parent categories (or none at all.

CATEGORY CATEGORY_GROUP
CAT_ID PARENT_CAT_ID
CHILD_CAT_ID

The tricky bit is that I don't want to store the CATEGORY_GROUP as a domain object, I simply want a CATEGORY object which has a list of Parent's and Children (each item of which is a CATEGORY object).

I can't seem to map from the CATEGORY table through the CATEGORY_GROUP table back to itself. The main problem is that the keys have different names.

Please help. Firstly is this possible. If so could you help me with the .hbm.cfg mappings, I'll sort the rest out.

Thank you

CB


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2003 2:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Look at the 5.9 section of the hibernate reference guide.
The many-to-many sample applies to you.

Quote:
You may specify a bidirectional many-to-many association simply by mapping two many-to-many associations to the same database table and declaring one end as inverse. Heres an example of a bidirectional many-to-many association from a class back to itself:

Code:
<class name="eg.Node">
    <id name="id" column="id"/>
    ....
    <bag name="accessibleTo" table="node_access" lazy="true">
        <key column="to_node_id"/>
        <many-to-many class="eg.Node" column="from_node_id"/>
    </bag>
     <!-- inverse end -->
    <bag name="accessibleFrom" table="node_access" inverse="true" lazy="true">
        <key column="from_node_id"/>
        <many-to-many class="eg.Node" column="to_node_id"/>
    </bag>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 4:48 pm 
Newbie

Joined: Thu Oct 02, 2003 5:39 pm
Posts: 4
Thanks for that,

As you'd expect the design has changed and we now only need a parent id in the main table. However I'm please I've managed to get it working both ways. Thanks again for your help.

Cheers

CB


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.