-->
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.  [ 1 post ] 
Author Message
 Post subject: Is there a way to make proxies truly Serializable?
PostPosted: Wed Jul 16, 2008 3:57 am 
Newbie

Joined: Wed Jan 31, 2007 10:18 pm
Posts: 10
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.


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

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.