-->
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: dealing with a many to many relationship with attributes
PostPosted: Mon Nov 14, 2005 6:25 pm 
Newbie

Joined: Mon Nov 14, 2005 5:57 pm
Posts: 11
The best way to explain my questions is with an example...
I have three classes(tables): Person, Event, and Event_Person. The reason for Event_Person is to break up the many to many relationship between Person and Event. The reason I am manually creating the table is because I would like to place attributes in that table (these attributes dont relate to any other table).
I can create Event_Person with a single primary key of event_person_id.
Is it possible to create this table with a compound primary key made up of the primary key of Event (Event_ID) and the primary key of Person (Person_ID). If so, which way is considered the best?
If the latter is possible and the prefered way, could you supply an example hibernate mapping for it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 6:39 pm 
Newbie

Joined: Wed Apr 13, 2005 8:10 am
Posts: 15
You map the m2n table as a normal class. use instead of <id>:

Code:
<composite-id class="PersonEventPK">
  <key-many-to-one name="person" class="Person" column="fkPerson"/>
  <key-many-to-one name="event" class="Event" column="fkEvent"/>
</composite-id>


alternatively, you can use, instead of <key-many-to-one/> the <key-property/>, which will hold a java value instead of a mapping class.

You also have to create the PK class (here PersonEventPK).

See here: http://www.hibernate.org/hib_docs/v3/re ... ompositeid

If that helped, please rate this answer.
Cheers,
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 9:06 pm 
Newbie

Joined: Mon Nov 14, 2005 5:57 pm
Posts: 11
assuming I do not have an existing database, that I am creating the database from the hibernate xml mapping files, would this be the recommended way to do it? or would it be best to just use a single primary key for the Person_Event class/table? (treat the Person to Person_Event as a typical one-to-many relationship).
The book Pro Hibernate says the <composite-id> element is...
Quote:
provided purely to support existing database schemas
.


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.