The error failed to lazily initialize a collection, no session or session was closed.
Many people ask almost the same question, but the resolution is not so intuitive. And, I think, one more question needs to be posed in order to describe the strange error message:
(I will not paste the source here because it is too long.)
DEBUG [main] (AbstractPlatformTransactionManager.java:365) - Creating new transaction with name [com.bee32.plover.orm.feaCat.FeaturePlayer.tcList]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG [main] (HibernateTransactionManager.java:493) - Opened new Session [org.hibernate.impl.SessionImpl@19006c9] for Hibernate transaction
DEBUG [main] (HibernateTransactionManager.java:523) - Not preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@19006c9]
DEBUG [main] (HibernateTemplate.java:397) - Found thread-bound Session for HibernateTemplate
Hibernate: select
...
ERROR [main] (LazyInitializationException.java:42) - failed to lazily initialize a collection, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
...
at java.util.HashSet.<init>(HashSet.java:116)
...
at org.hibernate.loader.Loader.list(Loader.java:2124)
...
at org.springframework.orm.hibernate3.HibernateTemplate$5.doInHibernate(HibernateTemplate.java:590)
...
DEBUG [main] (HibernateTemplate.java:422) - Not closing pre-bound Hibernate Session after HibernateTemplate
DEBUG [main] (AbstractPlatformTransactionManager.java:843) - Initiating transaction rollback
DEBUG [main] (HibernateTransactionManager.java:672) - Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@19006c9]
DEBUG [main] (HibernateTransactionManager.java:734) - Closing Hibernate Session [org.hibernate.impl.SessionImpl@19006c9] after transaction
As you can see, when lazy-fetch starts, an error has occurred, a thread-bound session already exists, and it is not yet closed until the transaction rolls back.
So why is he reporting what he has no session or session was closed?
EDIT
Related source:
@Transactional
public void tcList() {
for (Cat cat : dao.list()) {
System.out.println("Saved cat: " + cat);
}
}
source
share