I am using Hibernate2.1.2 and jdk1.4
I encounter problem with lazy initialization when I do these:
(1)Load an object.
(2)Call getter or setter of that object. (If this step, everything is fine).
(3)Call Session.clear()
(4)Call a getter, which returns a Collection, of the object
(5)Get Iterator from the return Collection and at this point the following
exception occurs:
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:206)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Set.iterator(Set.java:130)
at test.Test.doSomeTest(Test.java:126)
at test.Test.main(Test.java:30)
Caused by: java.lang.NullPointerException
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3217)
This is the code:
net.sf.hibernate.Session sess = sessionFactory.openSession(connection); MyClass myObj = (MyClass)sess.load(MyClassImpl.class, new Long(80224)); myObj.setDateTime(new java.util.Date());//if this is not here, no exception System.out.println("clearing session.... "); sess.clear(); System.out.println("cleared session.... "); Collection theAttributes = myObj.getListOfAttributes(); Iterator iter = theAttributes.iterator(); //Exception occurs here while(iter.hasNext()){ System.out.println(iter.next()); }
Anyone know of a solution to this problem ?
|