Hello all,
When I get a CGLIB entity proxy from hibernate, it is in an un-initialized state, but I can serialize it anyway... if I do, and then de-serialize it, by the moment I call any of its "getX()" methods, it tries to get initialized and... it fails with:
Code:
LazyInitializationException: could not initialize proxy - no Session
This is normal for me, but a little frustrating. Having a look at Hibernate's code, I found that
org.hibernate.proxy.pojo.cglib.SerializableProxy had the following "readResolve" method:
Code:
private Object readResolve() {
try {
return CGLIBLazyInitializer.getProxy(
entityName,
persistentClass,
interfaces,
getIdentifierMethodName==null ?
null :
getIdentifierMethodClass.getDeclaredMethod(getIdentifierMethodName, null),
setIdentifierMethodName==null ?
null :
setIdentifierMethodClass.getDeclaredMethod(setIdentifierMethodName, setIdentifierMethodParams),
componentIdType,
id,
null // <-- SESSION
);
}
catch (NoSuchMethodException nsme) {
throw new HibernateException("could not create proxy for entity: " + entityName, nsme);
}
}
In which I can see that
the last argument for that CGLIBLazyInitializer.getProxy call, which is the session, is null. Of course, that is why it fails telling that there is no Session from which to re-initialize the proxy... because the reconstructed proxy never gets one.
So here my question is: Is there any way to configure Hibernate (for instance, using a different kind of proxies) so that proxies being de-serialized get the equivalent to "SessionFactory.getCurrentSession()" instead of that "null" as a Session?
Of course that would mean being able to serialize in some way a reference to the SessionFactory instance being used, but... if there is a way, I think it would be really, really, really useful (for me, it would mean the difference between being able to make my entities Serializable or not).
Thanks.
Regards,
Daniel.