I'm wondering why I'm getting this exception while trying to get a list of object attached to an object, but I get lazy exception instead when the list is null:
For example:
protected static final String hqlRequest = "select user from User as user";
final StringBuilder sbHqlRequest = new StringBuilder(hqlRequest);
sbHqlRequest.append(" left outer join fetch user.listeCondition as condition");
sbHqlRequest.append(" left outer join fetch user.listeReponse as reponse");
sbHqlRequest.append(" left outer join fetch reponse.listeCat reponseCat");
sbHqlRequest.append(" left outer join fetch reponseCat.listeDomain listeDomain");
sbHqlRequest.append(" left outer join fetch reponse.listePlan reponsePlan");
sbHqlRequest.append(" where user.identifiant=? ");
return (User) session.createQuery(sbHqlRequest.toString()).setInteger(0, identifiant.intValue()).uniqueResult();
I'm getting a lazy when trying to access reponseCat.listeDomain
. In my DB there is no list, but I was waiting for null, not an exception.
Am I doing something wrong or is that how Hibernate works?
Exception:
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Cat.listeDomain - no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:191)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:183)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:118)
at org.apache.commons.collections.CollectionUtils.isEmpty(CollectionUtils.java:979)
at org.apache.commons.collections.CollectionUtils.isNotEmpty(CollectionUtils.java:992)
I noticied that: when I use the session passed in param, I get the lazy exception, but when I create a local session it works, but I get my objects associated with two different sessions!