-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many associations and extra attributes
PostPosted: Thu Apr 13, 2006 9:41 am 
Newbie

Joined: Thu Apr 13, 2006 4:41 am
Posts: 5
Hi All,

I have must what be a common problem and I found some indication of how togo about this in the Tips & Tricks but it didn't make sense to me...

so example tables :

Title (id,name)
Contributor (id,name)
TitleContributor (id,titleid,contributorid,role)
Role(id,name)

I'm sure that you understand the relationship above and that TitleContributor is the intersection table between Title & Contributor and that each relationship can be assigned to a role (Author,Illustrator,Editor etc)

When I load the Title object then I can easily get all assigned Contributor objects by making a mapping one-to-many relationship

but using this method I dont get my Role object, which is in a one to one relationship with TitleContributor...

Anyone knows the best way to go about this ?


Kind Regards,

Lennart


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 15, 2006 1:41 am 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
It looks like you want something like:

class Title {
private List<TitleContributor> contributors; ...
}
class Contributor...
class Role....
class TitleContributor {
private Contibutor contrib;
private Role role;
}

Title mapping includes:
<idbag name="contributors" table="TitleContributor">
<collection-id column="id" type="long">
<generator class="sequence"/>
</collection-id>

<key column="titleid">
<composite-element class="TitleContributor">
<many-to-one name="role" class="Role" column="role"/>
<many-to-one name="contrib" class="Contributor" column="contributorid"/>
</composite-element>
</idbag>

The TitleContributor class, being a composite element, doesn't get its own mapping file.

By the way, do you really need the id in the TitleContributor table? Couldn't you have a composite key of titleid,role and contributor? If so, you could change the List<TitleContributor> in Title into a Set, change the idbag in the mapping to a set, and remove the collection-id.


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