-->
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: Self-referential many-to-many mapping
PostPosted: Mon Aug 09, 2004 11:02 am 
Newbie

Joined: Mon Aug 09, 2004 10:53 am
Posts: 19
Hibernate version: 2.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

Debug level Hibernate log excerpt:

Hi, I have a question about self-referential many-to-many mapping.

I have a table called Product and wish to model an association between a single product and other products. I am using an association table, ProductAssociates, with a foreign key of productId pointing to what I call the host product in the Product table, and the other key of associateId poiting to the other products in the SAME Product table. Thus the association table doesn't associate 2 separate entity tables, but rather 2 different values within the same entity table. I'm having trouble setting up the mapping for this in Hibernate. Can someone advise me on how to do this?
Hence Product A can have products B, C, D as associates.
Product D can have A, B, E as associates, etc.
Its a many-to-many mapping.

Thanks for your help in advance,
Femi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 12:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sure, it would be collections on both sides of the association (many-to-many, right).

Product
---------------------------
PROD_ID
...


PRODUCTASSOCIATES
---------------------------
HOST_ID
ASSOCIATE_ID
...



Code:
<class name="Product"...>
    <id column="PROD_ID".../>

    <set name="associateProducts" inverse="false" lazy="true" table="PRODUCTASSOCIATES">
        <key column="HOST_ID"/>
        <many-to-many class="Product" column="ASSOCIATE_ID"/>
    </set>

    <set name="hostProducts" inverse="true" lazy="true" table="PRODUCTASSOCIATES">
        <key column="ASSOCIATE_ID"/>
        <many-to-many class="Product" column="HOST_ID"/>
    </set>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 10:58 am 
Newbie

Joined: Mon Aug 09, 2004 10:53 am
Posts: 19
This is helpful, thanks


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.