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