-->
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: Value of inverse=true?
PostPosted: Mon Aug 23, 2004 2:03 am 
Newbie

Joined: Thu Jun 24, 2004 8:22 am
Posts: 7
Hibernate version:2.1.4

Name and version of the database you are using:Oracle 8.1.7

Hi,

I have been reading a lot of documentation and faq's about the "inverse=true" settings in a bi-directional relationship. I have actually tried this out, and it works. But as we all know, it is not very convenient to add a child record, when you have a parent child relationship with 1000's of children. Problem is you have to load all children first (if you don't want to use a bag), which is of course unacceptable.

Now, in my situation, I have a parent and a child table. The child table has a foreign key (not-null) to the parent table. I am using a bi-directional relationship, but with inverse=false.
When I want to create a new child record, I simply do the following:

- load the parent
- create a new child, using the parent's id field to set the foreign key constraint (Child.setParentId(Parent.getId())
- save the child

When I look at the SQL Hibernate produces at child creation time, I see one simple insert statement, which succeeds. Now, I don't see how the "inverse=true" setting could optimize this? I am probably missing something, but for me everything works just fine with the default (inverse=false) setting??

Regards,
Johan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 2:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
(1) you don't need a parentId field in HIbernate, just the parent reference will do fine.

(2) if all you do is call child.setParent(parent), you will certainly only see one harmless insert statement, regardless of the setting for inverse

(3) it is when you *also* call parent.getChildren().add(child) that you would see the problem (you need to do this to maintain integrity at the Java level, if you plan to keep working with the parent object, or especially if the collection is cached)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 3:45 am 
Newbie

Joined: Thu Jun 24, 2004 8:22 am
Posts: 7
[quote="gavin
(3) it is when you *also* call parent.getChildren().add(child) that you would see the problem (you need to do this to maintain integrity at the Java level, if you plan to keep working with the parent object, or especially if the collection is cached)[/quote]

First of all, thank you for the quick answer!
Now, I can follow your way of thinking, but for the 3rd remark, I just don't see why I would want to call parent.getChildren().add(child), as this would load all children first (thousands in my case), and as a child can very easily be added by just mapping the parentId in the Child class, instead of referencing the parent class??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 3:47 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It wouldn't load the children if they are mapped as a bag (duplicates allowed). Think about it: A Set will always have to load them if you add an element, to guarantee uniqueness of the elements.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 3:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
He is asking why does he need to add it at all. Which is a good question. Strictly, you don't, unless the collection is cached in the second-level cache.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 4:39 am 
Newbie

Joined: Thu Jun 24, 2004 8:22 am
Posts: 7
When reading the forums, I still see a lot of questions regarding the proper use of "inverse=true" on bi-directional relationships. Also, in all (?) discussions and examples on this topic, I always see the parent.getChildren().add(child) appearing. Considering your answers, maybe it would be a good idea to add a very basic insert example like child.setParent(parent) on the FAQ pages, as this probably is sufficient in a lot of cases, and "solves" the annoying limitation of having to load all children first, before creating a child.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.