-->
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.  [ 8 posts ] 
Author Message
 Post subject: HowTo ? : many-to-many association with one property
PostPosted: Thu Jun 10, 2004 8:32 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
I'd like to know how to map a many-to-many association with one property.

For example we have the two tables A and B, and there is a many-to-many association between these two tables that gives us a new table called AB.

In AB there is the primary key from A and from B. And we also find one property in this association.

In the Hibernate reference doc I saw something like this :
<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"/> <!-- class attribute is optional -->
</composite-element>
</set>

But I'm not sure that is what I need. I tried this but I have some problems maybe because I made some mistakes when I implemented this solution.

Then if this the solution or if you have another solution, could you give me a simple example with the three tables I quoted (A, B and AB) ?


Thanks for your help.

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 11, 2004 2:08 am 
Beginner
Beginner

Joined: Thu May 13, 2004 5:51 am
Posts: 28
Say A: Teams, B: Users
Many users belong to a team and a user is part of many teams

in mapping for teams:
<set name="users" table="users_teams">
<key column="teamName"/>
<many-to-many column="loginId" class="User"/>
</set>

in mapping for users:
<set name="teams" table="users_teams">
<key column="loginId"/>
<many-to-many column="teamName" class="Team"/>
</set>

So you will get a extra table users_teams with (loginId, teamName) fields

Hope this helps,

--


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 11, 2004 2:14 am 
Beginner
Beginner

Joined: Thu May 13, 2004 5:51 am
Posts: 28
oops... sorry.. I did not pay much attention to the problem of a common property.. could you be more specific with your problem, a simple association diagram will help me understand better.

--
P.S: Visit this url for a fabulous example of mapping relationships:
http://www.xylax.net/hibernate/index.html


Top
 Profile  
 
 Post subject: Diagram to explain the problem
PostPosted: Fri Jun 11, 2004 3:09 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
Thanks for helping me :


------ ------- -------
A AB B
------ ------- -------
id_A id_A id_B
id_B
a_property



Relations : To one A there are several AB
To one B there are several AB
To one AB there is one A and one B

AB is the result of the many-to-many association between A and B
(To one A there are serveral B and to one B there are several A).

And a_property is a simple property like a name (String) or something like this (not a key).


But I success to implement the following mapping (see reference doc of Hibernate) :
<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"/> <!-- class attribute is optional -->
</composite-element>
</set>

and it seems to be the appropriate solution. But it's not sure I have to test it now with some records so I can't say if it is the right solution or not.

After testing it, if I see that's the right solution I will add a reply to my post in order to inform you.


Thanks,

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 11, 2004 3:12 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
Sorry, maybe the display will be better like this :


------
A
------
id_A

-------
AB
--------
id_A
id_B
a_property


-------
B
-------
id_B



Relations : To one A there are several AB
To one B there are several AB
To one AB there is one A and one B

AB is the result of the many-to-many association between A and B
(To one A there are serveral B and to one B there are several A).

And a_property is a simple property like a name (String) or something like this (not a key).


But I success to implement the following mapping (see reference doc of Hibernate) :
<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"/> <!-- class attribute is optional -->
</composite-element>
</set>

and it seems to be the appropriate solution. But it's not sure I have to test it now with some records so I can't say if it is the right solution or not.

After testing it, if I see that's the right solution I will add a reply to my post in order to inform you.


Thanks,

Nicolas


Top
 Profile  
 
 Post subject: Solution
PostPosted: Mon Jun 14, 2004 10:42 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
the following mapping (see reference doc of Hibernate) :
<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"/> <!-- class attribute is optional -->
</composite-element>
</set>
is the solution.

But you'll have to map the table AB twice (two classes with distinct names to map the table AB) otherwise you can't access AB by A and B.

Try this and you'll understand what I mean.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 11:14 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Map AB as an entity, not a composite element.
A 1-n AB n-1 B

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Solution
PostPosted: Thu Jun 24, 2004 6:13 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
The best solution was well in the reference documentation, I success to implement it :

Reference documentation
"A mapping like this allows you to map extra columns of a many-to-many association table to the composite element class. The following is a many-to-many association from Order to Item where purchaseDate, price and quantity are properties of the association:

<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"/> <!-- class attribute is optional -->
</composite-element>
</set>
</class>
"


Another solution consists to do like this :

in A declares a one-to-many with table AB
in AB declares a many-to-one with table A and a many-to-one with table B
in B declares a one-to-many with AB

But the best solution is to do as I first explained


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