Hibernate version:
Hibernate 3.2
Mapping documents:
Code:
<class name="ProjectItem" table="LM_PROJECT_ITEM" polymorphism="explicit">
<id name="id" column="M_ID" >
<generator class="uuid"/>
</id>
<property name="name" column="M_NAME" not-null="true"/>
<property name="description" column="M_DESCRIPTION" not-null="true"/>
<property name="date" column="M_CREATION_DATE" type="date"/>
<property name="parentId" column="M_PARENT_ID" />
<bag name="childs">
<key column="M_PARENT_ID"/>
<one-to-many class="com.asv.sdi.lm.model.ProjectItem"/>
</bag>
<join table="LM_EX_PROJECT_ITEM" optional="true" inverse="true">
<key column="EX_ID"/>
<property name="fk" column="EX_ID" insert="false" update="false"/>
<property name="howToTest" column="M_HOW_TO_TEST"/>
<property name="importance" column="M_IMPORTANCE"/>
<property name="initialEstimate" column="M_INITIAL_ESTIMATE"/>
<property name="realEstimate" column="M_REAL_ESTIMATE"/>
<property name="progress" column="M_PROGRESS"/>
</join>
</class>
DAO code: I am using Spring2
Version 1
Code:
public List getRootProjectItems() {
List items = getHibernateTemplate().find("from ProjectItem as pi left outer join fetch pi.childs where pi.parentId is null ");
return items;
}
Version 2
Code:
public List getRootProjectItems() {
List items = getHibernateTemplate().find("from ProjectItem as pi where pi.parentId is null ");
if (items != null) {
Iterator it = items.iterator();
while (it.hasNext()) {
ProjectItem projectItem = (ProjectItem) it.next();
getHibernateTemplate().initialize(projectItem.getChilds());
}
}
return items;
}
Hierarchy level is unrestricted. It works OK with Set Collection for childs.
Code:
<set name="childs">
<key column="M_PARENT_ID"/>
<one-to-many class="com.asv.sdi.lm.model.ProjectItem"/>
</set>
But it doesn't work for Bag. I have got an Exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.ProjectItem.childs, 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.PersistentBag.iterator(PersistentBag.java:249)
Test CodeCode:
public void testAllItems() {
List rootProjectItems = projectItemDAO.getRootProjectItems();
for (int i = 0; i < rootProjectItems.size(); i++) {
ProjectItem projectItem = (ProjectItem) rootProjectItems.get(i);
BigDecimal initialEstimate = projectItem.getInitialEstimate();
showCurrentHierarchyLevel(projectItem.getChilds());
}
}
private void showCurrentHierarchyLevel(Collection childs) {
Iterator it = childs.iterator();
while (it.hasNext()) {
ProjectItem projectItem = (ProjectItem) it.next();
if (projectItem.getChilds() != null) {
showCurrentHierarchyLevel(projectItem.getChilds());
}
}
}
Could you explain me strange behavior of Bag? What is the source of this error?