Hibernate version: 2.1.6
I have a situation where I have a one-to-many bi-directional relationship like this (abbreviated):
Code:
A User class that has a lazy relationship to portlets:
<hibernate-mapping>
<class
name="com.matrix.portal.bo.user.Person"
table="User"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="P"
>
<id
name="id"
column="User_ID"
type="int"
unsaved-value="0"
>
<generator class="native">
</generator>
</id>
<set
name="portlets"
table="User_PFunc_Link"
lazy="true"
inverse="true"
cascade="save-update"
sort="unsorted"
>
</class>
</hibernate-mapping>
And a Portlet class that maps something like this:
<hibernate-mapping>
<class
name="com.matrix.portal.bo.PortletFunction"
table="PortletFunction"
dynamic-update="false"
dynamic-insert="false"
lazy="true"
>
....
</class>
When one side of the mapping is marked as lazy="true" then I can use Hibernate.initialize() to init the proxies. However, when I have both sides marked as lazy and I want to retrieve the collection from the parent object I have to do something like this:
Code:
// Init the collection
Hibernate.initialize(user.getPortlets());
user.getPortlets().size();
// Now init each element in the collection since it is marked as a proxy
Iterator iter = user.getPortlets().iterator();
while (iter.hasNext()) {
PortletFunction element = (PortletFunction) iter.next();
Hibernate.initialize(element);
}
Is this the best way to do this, or should it be avoided with a bi-directional relationship?
Regards,
Mark[/code]