-->
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: mapping problem
PostPosted: Tue Oct 31, 2006 5:55 am 
Newbie

Joined: Tue Oct 31, 2006 5:19 am
Posts: 11
hi ,
i am new in Hibernate, getting confused by mapping a many-to-many relationship with a atrribute. if there's somebody can help?

have 3 tables:
Person: can attend many projects(PK:PersonID)
Project: contains many persons(PK:ProjectID)
the last table is Person_Project, which has PersonID(PK;FK), ProjectID(PK;FK), and a extra roleName.

in both of my person and Project mapping file i have this:
Person.hbm.xml
<set name="projects" table="TMP_PERS_SUBP" lazy="true">
<key column="PERSONID"/>
<many-to-many class="mapping.Project" column="PROJECTID"/>
</set>
Project.hbm.xml
<set name="persons" table="TMP_PERS_SUBP" lazy="true">
<key column="PROJECTID"/>
<many-to-many class="mapping.Person" column="PERSONID"/>
</set>

but what should i do with the attribute roleName?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 6:18 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: Tue Oct 31, 2006 6:32 am 
Newbie

Joined: Tue Oct 31, 2006 5:19 am
Posts: 11
thanks a lot. because i have not much idea of the whole thing yet, i will try the first option.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 6:35 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
I think using option 1 is a good choice for you since it also supports bidirectional navigation!

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 6:43 am 
Newbie

Joined: Tue Oct 31, 2006 5:19 am
Posts: 11
what should i do with the two PK(FK)s now? :-(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 6:49 am 
Newbie

Joined: Tue Oct 31, 2006 5:19 am
Posts: 11
i mean, do i need to generate a PK for this relationtable extra?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 2:14 pm 
Newbie

Joined: Tue Oct 31, 2006 12:25 pm
Posts: 1
Hi, I get exactly same problem with you.

Could you please explain me the first option?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 11:11 am 
Beginner
Beginner

Joined: Thu Nov 02, 2006 9:38 am
Posts: 32
Location: Belgium
Ting wrote:
i mean, do i need to generate a PK for this relationtable extra?


Use the two reference fields as primary key.


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.