-->
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.  [ 4 posts ] 
Author Message
 Post subject: Exception when adding obj to col. during lazy initialization
PostPosted: Mon Dec 22, 2003 6:16 am 
Newbie

Joined: Thu Dec 18, 2003 7:24 am
Posts: 4
Location: Belgium
Problem:
With bidirectional relationships, if you do not update both sides in your object model, you might have an object tree that is not consistent with the Database. (See [url]http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue[/url]

Solution:
Update child and parent at the same time as follows:

In the child:
[code]
public void setParent(Parent parent)
{
// Do something only if parent changes.
if (this.parent != parent) {
// Remove child from current parent (if not null)
if (this.parent != null) this.parent.removeChild(this);
// Set new parent
this.parent = parent;
// Add child to new parent (if not null)
if (parent != null) {
parent.addChild(this);
}
}
}
[/code]

In the parent:
[code]
public void addChild(Child child) {
this.children.add(child);
child.setParent(this);
}

public void removeChild(Child child) {
this.children.remove(child);
child.setParent(null);
}
[/code]

Comments:
This works fine if the children collection in the parent has the "lazy" flag set to "false". As long as this flag is set to "true", you get a [code]Failed to lazily initialize a collection[/code] exception. The reason is that you try to add a child to the parent collection during the lazy initialization.
If you update the setParent method as follows, it works fine:
[code]
public void setParent(Parent parent)
{
// Do something only if parent changes.
if (this.parent != parent) {
// Remove child from current parent (if not null)
if (this.parent != null) this.parent.removeChild(this);
// Set new parent
this.parent = parent;
// Add child to new parent (if not null)
if (parent != null) {

try {
if (((PersistentCollection)parent.getChildren()).wasInitialized()) {
parent.addChild(this);
}
} catch (Exception e) {

}
}
}
}
[/code]

Question:
Could it be possible to synchronize the collection "add" methods during the lazy initialization?

_________________
Eric.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 7:08 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It does not work because your session is closed: not the usual case. Capability of adding elements to a collection wo initializing it depends wether it's an indexed collection or not (see Chapter 13. Understanding Collection Performance).
If you manipulate collections (that need init to add) outside session, then initialize them before.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 7:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
PersistentCollection is internal implementation, prefer using Hibernate.isInitialized()

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 4:08 am 
Newbie

Joined: Thu Dec 18, 2003 7:24 am
Posts: 4
Location: Belgium
Thanks for your quick answer.
The code is cleaner and more generic with the Hibernate.isInitialized() method.

I tried with a list instead of a set. As expected the test on the collection initialization is useless, but I have each child twice in the collection (probably my mistake in the mapping files).

_________________
Eric.


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