I'm trying to fetch all records from a database table, as well as all required records from a subtable (implemented as a parent class with a collection of children) in order to generate a flat file of all records, but once I get out of my DAO, I get a LazyInitializationException (no session or session was closed) if I try to iterate through the children.
I figured I could get around this issue by simply iterating through all children within the DAO's fetch routine like so:
Code:
public List getAllDeep()
{
List result = getHibernateTemplate().find("from ParentTable");
for(Iterator recordIter = result.iterator(); recordIter.hasNext(); )
{
ParentClass record = (ParentClass)recordIter.next();
for(Iterator childIter = record.getChildren().iterator(); childIter.hasNext();)
{
ChildClass cc = ((ChildClass)childIter.next());
cc.getValue();
}
}
return result;
}
The above code works, but if I try to do the same thing again from the calling method, I get a LazyInitializationException. Shouldn't the data already be there since I manually iterated through every record, forcing the child to load?
Is there another way to do this? I can't use OpenSessionInView because it doesn't work with one of the frameworks we use.
My mapping files (simplified for illustration purposes):
Code:
<hibernate-mapping>
<class name="ParentClass" table="ParentTable">
<composite-id>
<key-property name="a" type="int" column="a"/>
<key-property name="b" type="int" column="b"/>
</composite-id>
<property name="name" type="string" column="name"/>
<set name="children" inverse="true">
<key>
<column name="a" />
<column name="b" />
</key>
<one-to-many class="ChildClass"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="ChildClass" table="ChildTable">
<composite-id>
<key-many-to-one name="parent" class="ParentClass" lazy="false">
<column name="a"/>
<column name="b"/>
</key-many-to-one>
<key-property name="value" type="string" column="value"/>
</composite-id>
<property name="anotherValue" type="string" column="anotherValue"/>
</class>
</hibernate-mapping>