[google-gson:639]: Serializing Hibernate Objects

1,657 views
Skip to first unread message

Norman Elton

unread,
May 5, 2010, 11:18:15 AM5/5/10
to google-gson
I'm using Gson to serialize objects coming from Hibernate. For
instance:

new GsonBuilder().registerTypeAdapter(Ford.class, new
FordSerializer()).registerTypeAdapter(Toyota.class, new
ToyotaSerializer()).create();

This is contrived example, but you get the point. If I create a Ford
object manually, the serializer works properly. However, when one is
loaded from Hibernate, it uses the default serializer instead of my
custom implementation. I believe this is due to Hibernate using a
javassist proxy to lazy-load the object. For instance, when I query
the class of the object, I get:

Car_$$_javassist_14

Has anyone else run across this behavior? Is there a good way to
properly serialize such objects? I have a few hacks in mind, but it
would deviate from Gson's nice registerTypeAdapter() behavior.

Thanks,

Norman

--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.

Jason Buberel

unread,
May 5, 2010, 12:21:20 PM5/5/10
to googl...@googlegroups.com
Q: Has anyone run into this before?
A: Yes. I certainly have.

Q: Is there a way to properly serialize?
A: I decided not to go down that path. Instead, I modified my usage to only serialize/unserialize the IDs of objects (either Long or Strong), and to then reconstitute the instances using session.load(ClassName.class, myID).

Another thing to try would be to use session.evict(instance) on the objects before running them through Gson. That should remove all of the javassist and related interceptors from the instances. 

-jason

Jason L. Buberel
ja...@buberel.org

albert_amg

unread,
Apr 4, 2011, 10:48:04 AM4/4/11
to googl...@googlegroups.com
I ran into this when loading hibernate objects with Session's load() method because it can return a proxy. Switching to get() method avoids the problem since it does not return a proxy.

If you are stuck with a proxy, you can get the "unproxied" object from the proxy and then pass the former to gson. HibernateUtil class (http://www.unitils.org/apidocs/org/unitils/reflectionassert/util/HibernateUtil.html) from Unitils library helps you with that. It has a method called getUnproxiedValue which does just that.
HibernateUtil.java

Joel

unread,
Apr 13, 2011, 3:52:15 AM4/13/11
to google-gson
I have not tried using Gson with Hibernate so pardon my ignorance if
this doesn't make sense.

With the Gson 1.7 release, we added a feature called hierarchical type
adapters. This allows a user of Gson to only specify a serializer for
some super class and it will get applied to all the subclasses.
Obviously, these type hierarchy adapters can be overridden by specific
type adapters or other more specific hierarchical type adapter (NB.
registration order of hierarchy type adapters does matter).

If this feature will work then the initial approach can be used with a
slight modification:
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Ford.class, new FordSerializer())
.registerTypeHierarchyAdapter(Toyota.class, new
ToyotaSerializer())
.create();

or better yet, possibly:
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Car.class, new CarSerializer())
.create();

If you have a spare cycle or two, feel free to try this out and if you
do then please let me know the results.

Regards,
Joel


On Apr 4, 7:48 am, albert_amg <albert....@gmail.com> wrote:
> I ran into this when loading hibernate objects with Session's load() method
> because it can return a proxy. Switching to get() method avoids the problem
> since it does not return a proxy.
>
> If you are stuck with a proxy, you can get the "unproxied" object from the
> proxy and then pass the former to gson. HibernateUtil class
> (http://www.unitils.org/apidocs/org/unitils/reflectionassert/util/Hibe...)
> from Unitils library helps you with that. It has a method called
> getUnproxiedValue which does just that.
>
>  HibernateUtil.java
> 4KViewDownload

harshal kshatriya

unread,
Feb 28, 2013, 12:38:46 AM2/28/13
to googl...@googlegroups.com
Thanks a lot Joel. Your post helped me to overcome the issue of serializing hibernate objects.
Reply all
Reply to author
Forward
0 new messages