-->
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.  [ 10 posts ] 
Author Message
 Post subject: many-to-many
PostPosted: Fri Oct 31, 2003 8:43 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Hi, I have need for bidirectional many-to-many. The doc says one end must be inverse=true, but I need to manipulate from either end. How do I do that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 8:46 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
From the spec:

"You may map a bidirectional one-to-many association by mapping a one-to-many association to the same table
column(s) as a many-to-one association and declaring the many-valued end inverse="true"."

OK - so the same appears to be true for one-to-many as well. Doesn't inverse=true imply that the relation can be maintained/persisted from only one end. But I need to maintain/persist from both ends.

My question applies to both <many-to-many> and <one-to-many>

Heeeelp!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 9:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can manipulate relationship both side if you use inverse="true". butw hen you add a object from one side, you must add on the otjer side

a.getBs().add(b);
b.getAs().add(a);

This is the expected behavior in Java.

Not setting inverse="true" will try to insert the relationship twice.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 10:02 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Thanks. But what is the behaviour if both sides have inverse=false?

I'm inclined to skip the entire inverse=x thing, cus' it confuses me. Are there instances where I MUST use inverse=true?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 10:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course. Actually, it is used behind the scenes to determine the order of SQL statements sent to the database. It is required to prevent foreign key constraint violations, like with a child (FK to parent) inserted before the parent.

Try to understand it, you can't do without.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 10:46 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Thanks Christian and other. My problem is that I try to automatically map from a UML model (having associations with muliplicity and navigability).

But there is inverse=x notion in UML, so I don't understand how to make this automatic :-(


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 10:49 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
UML is a language. It has nothing, repeat with me, nothing to do with object models and database schemas.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 01, 2003 7:21 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
So for bidirectional many-to-many, does it matter what side is inverse? Or will any side do?

Do you understand my problem? My task is to generate mapping from UML diagram (rational rose)

For a bidirectional many-to-many association, can I put inverse=true on either side?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 01, 2003 2:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
So for bidirectional many-to-many, does it matter what side is inverse?

No, it does not matter if in your code, you do what I did (set both side)
Code:
a.getBs().add(b);
b.getAs().add(a);

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 01, 2003 4:49 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
OK, then. So, given my Rational Rose model, it sound like this plan will work:

For bi-directional many-to-many relations, set inverse=true on one end - which one doesn't matter.

For bi-directional one-to-many, set the many-side inverse=true.

Sounds OK? Any caveats?


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