-->
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.  [ 6 posts ] 
Author Message
 Post subject: Unidir many-to-many with join table have "extra" attribute
PostPosted: Wed Feb 24, 2010 5:40 pm 
Regular
Regular

Joined: Tue Oct 10, 2006 2:21 pm
Posts: 58
Is there an example showing a unidirectional many-to-many association mapping with a join table, where the join table contains not just the ids of the two sides of the association, but also an "extra" attribute field containing data pertaining to the association of these two enties?

I see an example with a "clean" link table containing only the two ids, but not one with an extra attribute.

That is, instead of your example in 7.3.4 of the Hibernate 3.2 manual:

Code:
create table Person ( personId bigint not null primary key )
create table PersonAddress (
personId bigint not null,
addressId bigint not null,
primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )


I have something like this:

Quote:
create table Person ( personId bigint not null primary key )
create table PersonAddress (
personId bigint not null,
addressId bigint not null,
preferenceWeight int not null,
primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )

I'd like to do the equivalent mapping in section 3.4 but capturing the preferenceWeight attribute in each collection element. How to do this?


Top
 Profile  
 
 Post subject: Re: Unidir many-to-many with join table have "extra" attribute
PostPosted: Wed Feb 24, 2010 5:54 pm 
Regular
Regular

Joined: Tue Oct 10, 2006 2:21 pm
Posts: 58
Never mind. Google is my friend. :-)


Top
 Profile  
 
 Post subject: Re: Unidir many-to-many with join table have "extra" attribute
PostPosted: Wed Feb 24, 2010 6:00 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is usually easier to map the join table as a separate entity (eg. PersonAddress). If possible, use an surrogate id column and then use <many-to-one> to both Person and Address, and put a unique constraint on those two columns.


Top
 Profile  
 
 Post subject: Re: Unidir many-to-many with join table have "extra" attribute
PostPosted: Wed Feb 24, 2010 6:20 pm 
Regular
Regular

Joined: Tue Oct 10, 2006 2:21 pm
Posts: 58
Quote:
If possible, use an surrogate id column


Alas, not possible. I am using a composite key. This, of course, does not work because the two ID columns are repeated in the many-to-ones.

So what then?


Top
 Profile  
 
 Post subject: Re: Unidir many-to-many with join table have "extra" attribute
PostPosted: Wed Feb 24, 2010 6:28 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Then use the composite key with <key-many-to-one>, but it will be harder and require more code to maintain the relationship. Or you can use a composite key with <key-property> and then map the <many-to-one>:s with insert="false" and update="false".

You can find more info here:
http://docs.jboss.org/hibernate/stable/ ... ompositeid
http://docs.jboss.org/hibernate/stable/ ... ompositeid


Top
 Profile  
 
 Post subject: Re: Unidir many-to-many with join table have "extra" attribute
PostPosted: Wed Feb 24, 2010 6:32 pm 
Regular
Regular

Joined: Tue Oct 10, 2006 2:21 pm
Posts: 58
Quote:
Or you can use a composite key with <key-property> and then map the <many-to-one>:s with insert="false" and update="false".


Yup. That's the solution! Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.