I have 3 Entities named, Discipline, DisciplineMembership, DisciplineManagement:
one Discipline has many DisciplineMemberships
one Discipline has many DisciplineManagements
a DisciplineManagement has one to one relation with DisciplineMembership
the related part of associated hbm file for Discipline looks like this:
Discipline.hbm:
<class name="Discipline" table="DISCIPLINES">
<id name="id" type="java.lang.Long">
<generator class="increment" />
</id>
<set name="memberships" inverse="false" cascade="all-delete-orphan" >
<key column="disciplineId" not-null="true" />
<one-to-many class="DisciplineMembership" />
</set>
<set name="managements" inverse="false" cascade="all-delete-orphan">
<key column="disciplineId" not-null="true" />
<one-to-many class="DisciplineManagement" />
</set>
</class>
in a function called A, I want to iterate over DisciplineMemberships and DiscipilineManagements of a Discipline:
public void A(){
Discipline discipline = this.getObject();
for(DisciplineMembership mem: discipline.getMembers()){
System.out.print(mem);
}
for(DisciplineManagement man: discipline.getManagements()){
System.out.print(man);
}
}
first for statement runs correctly, but on second one, I catch LazyInitializationException as follows:
13-04-27 13:54:26,115 [hibernate.LazyInitializationException (<init>:42)] ERROR: failed to lazily initialize a collection of role: Discipline.managements, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Discipline.managements, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at ObjectManagementPart_Edit.A(ObjectManagementPart_Edit.java:6)
I am confused how can this be possible? the Discipline Session is open, I can iterate through its memberships, but when I want to Iterate through its managements, hibernate can not lazyInitialize them.
what should I do?
UPDaTE: I tried to use Hibernate.initialize() function, so the function A is now like this:
public void A(){
Discipline discipline = this.getObject();
Hibernate.initialize(discipline.getMembeships());
for(DisciplineMembership mem: discipline.getMemberships()){
System.out.print(mem);
}
Hibernate.initialize(discipline.getManagements());
for(DisciplineManagement man: discipline.getManagements()){
System.out.print(man);
}
}
the first use of Hibernate.initialize() for Membeships has no problem, but in second use for Managements I get this exception:
org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:474)
at org.hibernate.Hibernate.initialize(Hibernate.java:431)
at discipline.ObjectManagementPart_Edit.A(ObjectManagementPart_Edit.java:6)
Now I am more Confused :/
disciplinein? What doesthis.getObjectdo?discipline.getMemberships()but notdiscipline.getManagements()(and theSessionwas closed) when you call thatA()method. Try this: make sure you get/send a reference of theSessionin/toA(), and, before (or inside) eachforloop, do aSystem.out.println(session.isOpen());. This will solve, or at least narrow down, the problem.