-->
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.  [ 2 posts ] 
Author Message
 Post subject: Failed to lazily initialize a collection
PostPosted: Fri Apr 02, 2004 1:54 am 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
some would say this problem has already been mentioned lots of times! but i couldn't find a convincing answer.

i do a simple test where Platform->ManagedElement is one2one
and ManagedElement->Cpu is a <one-to-many> bag

Code:
   public static void main(String[] args)
   {
      HibernateTest.hibernateInit();

      populateSampleData();
      
      List l1 = findPlatforms("123457");
      System.out.println(l1.size());
      
      serializePlatform(l1);
      
      List l2 = deserializePlatform();
      Platform p = (Platform)l2.get(0);
      List cpus = p.getManagedElement().getCpus();
      cpus.size();
   }


obviously the deserialized object graph has no session context. and during the 'find' i make sure that Cpus are lazy loaded (i.e. in this case Cpus are not loaded at all).

looks like there's no way to passivate hibernate's collection (here for e.g. Bag) and use it later like a regular collection since every operation calls PersistentCollection.read() which calls PersistentCollection.initialize(..)

Code:
   protected final void initialize(boolean writing) {
      if (!initialized) {
         [b]if ( isConnectedToSession() )[/b]  {
            try {
               session.initializeCollection(this, writing);
            }
            catch (Exception e) {
               LogFactory.getLog(PersistentCollection.class).error(
                  "Failed to lazily initialize a collection", e
               );
               throw new LazyInitializationException("Failed to lazily initialize a collection", e);
            }
         }
         else {
            throw new LazyInitializationException("Failed to lazily initialize a collection - no Session");
         }
      }


this causes the following
Code:
SEVERE: Failed to lazily initialize a collection - no Session
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
   at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:213)
   at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:69)
   at net.sf.hibernate.collection.Bag.size(Bag.java:231)


the layer that uses this serialized collection simply traverses the graph without having to know about sessions and transactions.

i would expect 'cpus' to be null and get a NullPointerException at runtime rather than a LzayLoad exception.

comments?

ravi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 02, 2004 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually the object is not Null, it's just not initialized. Different states here

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.