-->
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.  [ 4 posts ] 
Author Message
 Post subject: Attaching a property to a many-to-many realtionship
PostPosted: Mon Nov 06, 2006 11:56 pm 
Newbie

Joined: Wed Oct 26, 2005 11:55 pm
Posts: 14
Hibernate version: 3.1.3
Name and version of the database you are using: MySQL 5

I've been looking through the reference docs (especially the section on advanced collection mapping), but don't see anything that talks about this particular situation:

I need to map a many-to-many relationship between instances of the same entity type. The twist is that I need to have a "note" attached to each such association.
In non-abstract terms: my entities are Books, and any Book can be associated with any number of other Books. When such an association exists, I need to persist a note about that particular relationship.

If it were just a matter of the database schema, I would just add a column to the relation table. Can this be done with just a <set> and <many-to-many> mapping? I don't think so, because what would Hibernate do with the data in the "note" column?
So my thinking is that I'll have to model the relationship itself as an entity - is that the way to go? If so, what is the best way to query for all such entity instances given a particular Book, and can it be made part of the lazy fetch of the Book in question or must it be a separate query?
Finally, an example using the Criteria API would probably help me a great deal, if you've got the inclination to go that far.

Thanks in advance,
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 5:40 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
a question, how do you do the same thing in Java?

how do you access the note property when you only have books and collection of books?

I believe you must map the join table (with additional fields) to anothere entity.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 5:46 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
You have two options:

1) An intermediate Entity Class for the join table with one-to-many mappings


2) Using a collection of components which is described here:
http://www.hibernate.org/hib_docs/v3/re ... ollections
You should be aware that bidirectional navigation is NOT supported with this approach.
This mapping is copy pasted from the Hibernate documentation, I have just marked the interesting part of this kind of mapping.

<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.Purchase">
<property name="purchaseDate"/>
<property name="price"/>
<property name="quantity"/>
<many-to-one name="item" class="eg.Item"/>
</composite-element>

</set>
</class>

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 4:07 pm 
Newbie

Joined: Wed Oct 26, 2005 11:55 pm
Posts: 14
MikePloed wrote:
2) Using a collection of components which is described here:
http://www.hibernate.org/hib_docs/v3/re ... ollections


Thanks - I was paying attention to Chapters 6 & 7, but missed this part of Chapter 8 somehow. It looks promising - thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.