Hibernate version: 2.1.6
Mapping documents:
Code:
<set
name="representatives"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>
<key
column="fk_company"
>
</key>
<one-to-many
class="com.xyz.CompanyRepresentativeHPB"
/>
</set>
Code between sessionFactory.openSession() and session.close():Code:
// create fi1 in another session...
session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
session.lock(fi1, LockMode.NONE);
System.out.println(fi.getRepresentatives().size());
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
Hmm, maybe I still don't understand the semantics of lock(), but I am facing a strange behaviour.
If I reattach the POJO fi1 to the current session using
Code:
lock(fi1, LockMode.NONE)
then the lazy collections are null and I get a NullPointerException if I want to access the collection.
If I use
Code:
refresh(fi1, LockMode.NONE)
then the lazy collections are initialized as empty sets, which is what I would expect from lock, too.
As far as i understand the rules are...
* Use lock() if the object is clean
* Use refresh() if the object is dirty
My object is clean, so I would like to lock it back into the session. Why can't I lazy-load the collections after lock ? Is there any workaround for this ?