-->
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.  [ 7 posts ] 
Author Message
 Post subject: Inverse=TRUE QUESTION
PostPosted: Wed Mar 08, 2006 1:08 pm 
Beginner
Beginner

Joined: Mon Feb 27, 2006 11:16 am
Posts: 25
To


Last edited by jadeite100 on Thu Nov 09, 2006 3:13 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 8:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Inverse attribute tell hibernate which side of the relation is responsible for maintaining the link between the entities. Thta is the database updates for the foreign key data. So the guideline is:
Bi-directional - one side (one-to-many) is set to inverse=true.
Uni-directional - (one-to-many) needs to be set to inverse=false

In the case of a many-to-many again only one side should be inverse=true while the other should be inverse false.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 8:10 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
David & jadeit100,

I tried to summarize the "inverse" concept in a prior posting - David please comment on this summary if it is incorrect. I appreciate it.

quoted from:
http://forum.hibernate.org/viewtopic.php?t=955973&highlight=inverse

Quote:
I admit the "inverse" concept is a bit allusive - generally speaking I end up
using the following guidelines, and then testing to make sure it is saving the way I want:

For bi-directional relationships, 1-M and M-M make sure you put the inverse attribute in the collection tag. For M-M it doesn't matter, but for 1-M it goes in the only collection tag involved in this relationship.

Inverse tells hibernate which end of the relationship to get information about the link... so if you put inverse="true" on your ONE side ie.
<class name="parent"> <!-- this is the ONE side -->
<set name="children" ...inverse="true">
</set>
Then you are telling hibernate that the "many" side ie, Child class is managing the "link" info (in a bi-directional association). In a non-bidirectional one-to-many, ie the parent just has many children, and the child object doesn't know about the parent - Hibernate just defaults to the right management, ie managed by the parent.

I hope this makes sense, and please, someone correct me if I am wrong or misleading with this.

Here is a quote from the docs:

section 1.3.6. Working bi-directional links in reference.pdf

All bi-directional associations need one side as inverse. In a one-to-many association it has to be the many-side, in many-to-many association you can pick either side, there is no difference.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject: Inverse=TRUE QUESTION
PostPosted: Wed Mar 08, 2006 10:12 pm 
Beginner
Beginner

Joined: Mon Feb 27, 2006 11:16 am
Posts: 25
david wrote:
Inverse attribute tell hibernate which side of the relation is responsible for maintaining the link between the entities. Thta is the database updates for the foreign key data. So the guideline is:
Bi-directional - one side (one-to-many) is set to inverse=true.
Uni-directional - (one-to-many) needs to be set to inverse=false

In the case of a many-to-many again only one side should be inverse=true while the other should be inverse false.

Hope this helps.


To Whom It May Concern:

I am not clear from your point about Bi-directional and Uni-directional.
What is the difference?


Yours,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 3:34 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
Quote:
Then you are telling hibernate that the "many" side ie, Child class is managing the "link" info (in a bi-directional association). In a non-bidirectional one-to-many, ie the parent just has many children, and the child object doesn't know about the parent - Hibernate just defaults to the right management, ie managed by the parent.


Parent(1) to (M)Children

uni-directional:
parent has a set of children
and Children don't have a reference back to Parent, ie there is no attribute defined in the Child class that is of type Parent, and thus pointing to the parent.

bi-directional:
The Child has the Parent attribute and it refers to the "owning" parent.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject: Association bidirectional
PostPosted: Sun May 28, 2006 3:38 pm 
Newbie

Joined: Sun May 28, 2006 3:19 pm
Posts: 2
Hello! Sory for my english..
Is necessary in every relation bidirectional use inverse = true?

Perhaps i can have this association bidirectional:one category have many items and one item is in many categories. And i don't putt inverse = true. in the mapping file.

Por example I crete a new Item and then associate with a category existing in the database.

tx = session.beginTransaction();
Item item = new Item();

..................

Category cat = (Category)session.get(Category.class,new Integer(1));
..........


cat.getItems.add(item);
save (item);
tx.commit();

Then in other session i can retrieve all the categories of that item call
item.getCategories();
out of the need associate item.getCategorias().add(cat)
And the relation is bidirectional. I have a set of categories in the bean Item, and i have a set of item in the bean Categories.

Can somebody give me a real example when its necesary to associate in both side...Por example cat.getItems.add(item); and then item.getCategories().add(cat);

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
AS you are obviously aware you don't have to set both sides of the relation in the domain object to get the (future) database reads to act as they are bi-directional. If you don't then the domain model is inconsistent with the database - at least for the short time of the session. Is this an issue? Thats up to you as the developer to decide. As far as not using inverse=true on a bi-directional link there can be performance implications as well as the above issue. It is well documented: see http://www.hibernate.org/hib_docs/v3/reference/en/html/example-parentchild.html
for further details.


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