-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Lazy collections are null when re-attaching with lock()
PostPosted: Fri Sep 17, 2004 6:34 pm 
Beginner
Beginner

Joined: Tue Aug 17, 2004 5:06 am
Posts: 46
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 ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 8:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Um. Why are your collections null? You must have set them to null.


Top
 Profile  
 
 Post subject: No
PostPosted: Fri Sep 17, 2004 11:05 pm 
Beginner
Beginner

Joined: Tue Aug 17, 2004 5:06 am
Posts: 46
No, I don't think that I set them to null.

This is my code...

Code:
         FinancialInstitutionHPB fi = null;
         Session session = HibernateUtil.getSession();
         HibernateUtil.beginTransaction();
         // create a new instance and set some simple properties (not the collections...)
         fi = CompanyTestdataFactory.createFinancialInstitution();

         session.save(fi);
         HibernateUtil.commitTransaction();
         HibernateUtil.closeSession();

         session = HibernateUtil.getSession();
         HibernateUtil.beginTransaction();
         session.lock(fi, LockMode.UPGRADE);
         CompanyRepresentativeHPB rep = new CompanyRepresentativeHPB();
         rep.setCompany(fi);
         fi.getRepresentatives().add(rep);
         HibernateUtil.commitTransaction();
         HibernateUtil.closeSession();

         HibernateUtil.commitTransaction();
         HibernateUtil.closeSession();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 11:42 pm 
Beginner
Beginner

Joined: Tue Aug 17, 2004 5:06 am
Posts: 46
Stupid me. Well, at least it's 5 a.m. here...

For whatever reason I expected that Hibernate would magically create proxy collections for me. My objects did have null-collections. Once I set them to new HashSets, everything worked fine....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.