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