-->
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.  [ 8 posts ] 
Author Message
 Post subject: Issue related to proxy serialization
PostPosted: Wed Feb 08, 2006 8:36 am 
Newbie

Joined: Wed Jan 28, 2004 5:18 am
Posts: 11
Hi,

I've recently had this issue during the migration of an application from hibernate 2 to hibernate 3, which I want to discuss on this forum with other Hibernate users:

I implemented the following form of entity serialization: during serialization, I write only the entity id (primary key) on the stream. For deserialization, I implement readResolve(), in which I get the current hibernate session, and get the entity based on the serialized primary key. This way, I have a clean serialization mechanism, not needing to bother with locking the entity with the session after deserialization, etc.

This mechanism worked fine for me with hibernate 2. However, with hibernate 3 I run into the following situation: if the entity has an unitialized proxy (e.g. was obtained through session.load), only the proxy gets serialized, without being initialized first. At deserialization, the proxy gets deserialized, but without readResolve being called, and the proxy is obviously not linked to the current session.

From my point of view, this is a bug in hibernate 3, as the proxy is not as transparent as it should be. I tried filling a bug report on this issue, but it was rejected (http://opensource2.atlassian.com/projects/hibernate/browse/HHH-1443).

What do you think? Is this really an issue with Hibernate 3, or not?
And does anybody have a sugestion for a workaround to achieve the serialization mechanism I described (and which worked in hibernate 2)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 4:23 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Where do you call your readResolve() method from ?
I use the Extrenalizable interface in conjunction with an ObjectOuputStream to serialize objects loaded from Hibernate and it works transparently whether the proxy is initialized or not.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 10:44 am 
Newbie

Joined: Wed Jan 28, 2004 5:18 am
Posts: 11
Quote:
Where do you call your readResolve() method from ?

I don't call it, the JVM (normally) calls it. It's the standard Java serialization mechanism (see http://java.sun.com/j2se/1.4.2/docs/api/java/io/Serializable.html).
I have to say I have not tried to implement Externalizable, but with Serializable it's only the uninitialized proxy that gets serialized, and writeObject is not even called.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 12:02 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
First, you may try setting the lazy-load default to false in each of your mappings. Hibernate 3 differs from Hibernate 2 in that it has lazy loading (via CGLIB-generated proxies) as the default. Your old system probably fully loaded the object graph without proxies.

More generally, we have not found that trying to get proxies to work in a distributed way is feasible. (and beleive me we tried). The standard pattern seems to be that you touch/initialize all the stuff you need during the server call while connected to the session. Setting lazy=false in all the right places is just one way to force that.

Again, set lazy=false at the class level is easiest so you don't need to mark every relationship as non-lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 4:03 am 
Newbie

Joined: Wed Jan 28, 2004 5:18 am
Posts: 11
Quote:
First, you may try setting the lazy-load default to false in each of your mappings. Hibernate 3 differs from Hibernate 2 in that it has lazy loading (via CGLIB-generated proxies) as the default. Your old system probably fully loaded the object graph without proxies.

Nope, it did not. I use lazy load and proxies almost always - I carefully monitored the generated SQL queries. Using lazy=false is not an option for me, neither is disableing proxies.
And I am really sure that proxy serialization worked smooth in hibernate 2, which I can not say about Hibernate 3.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 4:15 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You must use custom proxies to implement this hack in Hibernate3, hibernate replaces uninitialized proxy with fake handle itself.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 6:10 am 
Newbie

Joined: Wed Jan 28, 2004 5:18 am
Posts: 11
Thanks for the suggestion.
Any hints where I should start digging from? I do not remember finding anything about custom proxies in Hibernate manual...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 6:51 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
See custom tuplizer.


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