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:
|