-->
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.  [ 8 posts ] 
Author Message
 Post subject: Call Hibernate.isInitialized() before adding to Collection?
PostPosted: Mon Mar 01, 2004 10:10 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Looking at the chapter on Parent/Child relationships, the recommended way of adding a child is with the following code:

Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(c);


My question is, wouldn't it be more efficient to do a Hibernate.isInitialized() on the collection first and only add to it if it is already initialized? Why force an additional SQL query to get the collection if it may not even be needed (e.g. when the add() is called, this would force a SQL query to lazily load the collection before the new element can be added)?

So my code would become:

Code:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
if (!Hibernate.isInitialized(p.getChildren()) {
  p.getChildren().add(c);
}
session.save(c);


If the program needs to collection later on, the lazy init SQL could be performed then, and the new element would be included in that result set.

Are there any possible problems doing it this way, or is not recommeded for any reason?

Thanks,
Daniel[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 10:11 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Correction, in the code it should be:

Code:
if (Hibernate.isInitialized(p.getChildren()) {


without the !.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Don't think this works.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 10:26 am 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Could you please expalin why this may not work?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 10:15 am 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
Anybody have any thoughts on why this may not work? I have been using this pattern in my code and so far have not noticed any negative consequences, but does anybody see any issues?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 6:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm intend to disagree Michael, I would say it works if you properly set inverse="true".
This is bug-prone anyway, if you change your mapping a bit.
Michael, what do you had in mind ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 8:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It works, you can do it if you like.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 8:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
(If only add() didn't have a return value, I could do it internally!)


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