Hello,
We are having a problem implementing lazy loading while creating a tree hierarchy with hibernate. Our hbm files are created with annotations, but I'll start with the hbm directly as the example... it has been copied below.
What I'm currently seeing is when a PccTier is accessed, either from the code below, or using session.get(PccTier.class, Long), initially the parentTier is successfully coming back lazy, and the children collection is not. At the point in which the parent is accessed with a pccTier.getParent() call, the parent is instantiated, and it's parent and children are not lazy. This results in hibernate accessing the entire tree anytime the parent of a tier is accessed.
Also, please note that this definition contains a joined-subclass, and the children collection may contain items of this type. I have assumed that the lazy=true directive on PccTier also applies to PccRoom since it is a joined-subclass. If this assumption is incorrect, please let me know.
I would like to get both the children of a tier to be loaded lazily, as well as the parent of an instantiated parent. Any thoughts?
Thanks in advance!!
- Mike Brindamour
Hibernate version: 3.0.5
Mapping documents:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class lazy="true" table="PCCPOLICYCONTROLTIER" name="com.enterasys.netsight.pcc.api.model.PccTier">
<id name="policyControlTierId" type="java.lang.Long">
<generator class="native"/>
</id>
<version unsaved-value="undefined" name="version"/>
<property name="rootNode"/>
<property name="tierDescription" length="255"/>
<property name="tierName" length="64"/>
<many-to-one column="id" cascade="none" class="com.enterasys.netsight.pcc.api.model.PccTier" name="parentTier"/>
<set inverse="true" cascade="delete-orphan" outer-join="false" lazy="true" name="childTiers">
<key/>
<one-to-many class="com.enterasys.netsight.pcc.api.model.PccTier"/>
</set>
<joined-subclass name="com.enterasys.netsight.pcc.api.model.PccRoom" table="PCCROOM">
<key/>
<set cascade="all" outer-join="false" lazy="true" table="PCC_ROOM_ROLEGROUPS" name="allowedRoleGroup">
<key column="PccRoom_ID"/>
<many-to-many class="com.enterasys.netsight.pcc.api.model.PccPolicyGroup" column="PccPolicyGroup_ID"/>
</set>
<set cascade="all-delete-orphan" outer-join="false" lazy="true" name="ports">
<key column="ROOM_ID"/>
<one-to-many class="com.enterasys.netsight.pcc.api.model.PccPort"/>
</set>
<many-to-one unique="false" column="DEFAULT_PCCPOLICYROLE_ID" outer-join="false" class="com.enterasys.netsight.pcc.api.model.PccPolicyRole" lazy="true" name="defaultPolicyRole"/>
</joined-subclass>
</class>
<query name="pcc.policycontroltier.byname"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccTier as tier where tier.tierName = :name]]></query>
<query name="pcc.policycontroltier.all"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccTier]]></query>
<query name="pcc.policycontroltier.root"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccTier as tier where tier.parentTier = null AND tier.rootNode = true]]></query>
<query name="pcc.policycontroltier.top"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccTier as tier where tier.parentTier = null order by tier.tierName desc]]></query>
<query name="pcc.policycontroltier.byid"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccTier as tier where tier.policyControlTierId = :id]]></query>
<query name="pcc.policycontroltier.path"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccTier as tier where tier.tierName = :name AND tier.parentTier.tierName = :parent]]></query>
<query name="pcc.pccroom.byid"><![CDATA[from com.enterasys.netsight.pcc.api.model.PccRoom as tier where tier.policyControlTierId = :id]]></query>
<query name="pcc.pccroom.byportid"><![CDATA[select room from com.enterasys.netsight.pcc.api.model.PccRoom as room join room.ports port where port.portId = :portId]]></query>
<query name="pcc.pccroom.bygroupid"><![CDATA[select room from com.enterasys.netsight.pcc.api.model.PccRoom as room join room.allowedRoleGroup rolegroup where rolegroup.id = :groupId]]></query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
PccTier tier = null;
Query query = hsession.getNamedQuery("pcc.policycontroltier.byid");
query.setLong("id", id);
tier = (PccTier) query.uniqueResult();
Full stack trace of any exception that occurs: N/A
Name and version of the database you are using: MySql 4.1.7