-->
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.  [ 5 posts ] 
Author Message
 Post subject: "Failed to lazily initialize a collection" Excepti
PostPosted: Thu Jan 05, 2006 11:32 pm 
Newbie

Joined: Wed Jul 21, 2004 9:41 pm
Posts: 11
Hi,
Here is an exception that is thrown when we try to migrate it to Hibernate3.1.
The code works fine in Hibernate3.0.
After some debug works, I found that the exception is thrown because of this line of code.

class AbstractPersistentCollection {
...
private final boolean isConnectedToSession() {
return session!=null &&
session.isOpen() &&
session.getPersistenceContext().containsCollection(this);
}
}

Some of us prepare the Set for the object manually.
Like:
if (step.getComp_id().getApproval().getApprovalSteps() == null) {
step.getComp_id().getApproval().setApprovalSteps(new HashSet());
}

step.getComp_id().getApproval().getApprovalSteps().add(step);
session.add(step);

The question is:
What is the best way to overcome this problem?
Thank you very much in advance.

Hibernate version:
3.1

Mapping documents:
Just a lazy collection in a xml file.

Code between sessionFactory.openSession() and session.close():
public Collection getApprovers() throws GTSystemException {
Collection dataCol = new ArrayList();
Collection asCol = this.getApprovalSteps();
for (Iterator it = asCol.iterator(); it.hasNext();) {
ApprovalStep as = (ApprovalStep)it.next();
[i]for (Iterator it1 = as.getApprovalStepUsers().iterator();[i]
it1.hasNext();) {
ApprovalStepUser asu = (ApprovalStepUser)it1.next();
dataCol.add(asu.getUnderlyingBusinessObject());
}
}

return dataCol;
}


Full stack trace of any exception that occurs:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
at com.gearteks.framework.model.approval.Approval.getApprovers(Approval.java:787)

Name and version of the database you are using:
SQLServer 2000 SP4

The generated SQL (show_sql=true):
N/A

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 12:03 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I presume that getApprovalSteps() is closing the session, so you'll need to inflate the users before the method returns. Calling Hibernate.initialize on the collection of users will do the trick.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 9:49 pm 
Newbie

Joined: Wed Jul 21, 2004 9:41 pm
Posts: 11
Thanks for your reply.
But when I debug inside, it shows that the session.isOpen() return true;
The problem is the
getPersistentContext().containsCollection(this); return false.

So I think the session actually is NOT closed.
Our environment is:
We haven't used cascade insert/update

So when doing a Master/Detail insertion.
We do
deleteUsingQuery("select all children");

parent.getChildren().clear();
session.save(child1);
parent.getChildren().add(child1);

session.save(child2);
parent.getChildren().add(child2);
...

I test the code yesterday. When I add session.refresh(parent);
The exception is gone.

Maybe our approach is not allowed in Hibernate3.1?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 12:04 am 
Newbie

Joined: Wed Jul 21, 2004 9:41 pm
Posts: 11
Urr...
Any suggestion?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 12:07 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
None, sorry...


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