Could not serialize object cause of HibernateProxy
You can do without manually unproxying everything by using a custom TypeAdapter
.
Something along these lines:
/**
* This TypeAdapter unproxies Hibernate proxied objects, and serializes them
* through the registered (or default) TypeAdapter of the base class.
*/
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson) : null);
}
};
private final Gson context;
private HibernateProxyTypeAdapter(Gson context) {
this.context = context;
}
@Override
public HibernateProxy read(JsonReader in) throws IOException {
throw new UnsupportedOperationException("Not supported");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
// Retrieve the original (not proxy) class
Class<?> baseType = Hibernate.getClass(value);
// Get the TypeAdapter of the original class, to delegate the serialization
TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
// Get a filled instance of the original class
Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation();
// Serialize the value
delegate.write(out, unproxiedValue);
}
}
To use it you must first register it:
GsonBuilder b = new GsonBuilder();
...
b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
...
Gson gson = b.create();
Notice that this will recursively initialize every proxy you have in the object hierarchy; since however you have to serialize the whole data, you should have done that anyway.
How does this work?
GSON contains a number of TypeAdapterFactory
implementations, for various types (primitive types, common types like String
or Date
, lists, arrays...). Each factory is asked if it is able to serialize a certain Java type (the parameter to create
is a TypeToken
instead of a Class
in order to capture possible information about generic types, which Class
does not have). If the factory is able to serialize/deserialize a type, it responds with a TypeAdapter
instance; otherwise it responds with null
.
HibernateProxyTypeAdapter.FACTORY
verifies whether type implements HibernateProxy
; in that case, it returns an instance of HibernateProxyTypeAdapter
for serialization.
The write
method is called when an actual object has to be serialized; the adapter extracts the original type of the underlying object, and asks GSON for the standard TypeAdapter
for the original type, which generally is a ReflectiveTypeAdapter
.
Then it retrieves an instance of the original class, instead of directly using the proxy. This is necessary because ReflectiveTypeAdapter
accesses directly to fields, instead of using getters; accessing to the fields of a proxied object does not work, and is a classical Hibernate pitfall.
As a possible performance improvement, the delegate TypeAdapter
should be acquired in the create
method. I found out that calling getSuperclass()
on the proxy Class
appears to yield the original base class. The code can then become:
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return (HibernateProxy.class.isAssignableFrom(type.getRawType())
? (TypeAdapter<T>) new HibernateProxyTypeAdapter((TypeAdapter)gson.getAdapter(TypeToken.get(type.getRawType().getSuperclass())))
: null);
}
};
private final TypeAdapter<Object> delegate;
private HibernateProxyTypeAdapter(TypeAdapter<Object> delegate) {
this.delegate = delegate;
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
delegate.write(out, ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation());
}
No serializer found for class org.hibernate.proxy.pojo.javassist.Javassist?
I had a similar problem with lazy loading via the hibernate proxy object. Got around it by annotating the class having lazy loaded private properties with:
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
I assume you can add the properties on your proxy object that breaks the JSON serialization to that annotation.
The problem is that entities are loaded lazily and serialization happens before they get loaded fully.
Hibernate.initialize(<your getter method>);
No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
I came across this error while doing a tutorial with spring repository. It turned out that the error was made at the stage of building the service class for my entity.
In your serviceImpl class, you probably have something like:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.getOne(id);
}
Change this to:
@Override
public YourEntityClass findYourEntityClassById(Long id) {
return YourEntityClassRepositorie.findById(id).get();
}
Basically getOne is a lazy load operation. Thus you get only a reference (a proxy) to the entity. That means no DB access is actually made. Only when you call it's properties then it will query the DB. findByID does the call 'eagerly'/immediately when you call it, thus you have the actual entity fully populated.
Take a look at this: Link to the difference between getOne & findByID
Strange Jackson exception being thrown when serializing Hibernate object
It's not ideal, but you could disable Jackson's auto-discovery of JSON properties, using @JsonAutoDetect
at the class level. This would prevent it from trying to handle the Javassist stuff (and failing).
This means that you then have to annotate each getter manually (with @JsonProperty
), but that's not necessarily a bad thing, since it keeps things explicit.
Spring MVC controller Json response, hibernate proxy error
The exception is thrown by com.google.gson.internal.bind.TypeAdapters when GSON is trying to serialize variable 'events' to Json.
This happens, cause
eventService.listAllEvents()
returns not a list already containing all events, but hibernate proxy that will do that lazy, when the list is actually used.
GSON does not know how to serialize that proxy.
Hibernate.getClass should initialize the underlying object as a side effect.
You need to call it also for the List 'events' itself, not only for every single event. The List can be a hibernate proxy also.
You may find more info on that topic at
Could not serialize object cause of HibernateProxy
Get HibernateProxy at one object
This Adapter is my solution.
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson) : null);
}
};
private final Gson context;
private HibernateProxyTypeAdapter(Gson context) {
this.context = context;
}
@Override
public HibernateProxy read(JsonReader in) throws IOException {
throw new UnsupportedOperationException("Not supported");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
// Retrieve the original (not proxy) class
Class<?> baseType = Hibernate.getClass(value);
// Get the TypeAdapter of the original class, to delegate the serialization
TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
// Get a filled instance of the original class
Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation();
// Serialize the value
delegate.write(out, unproxiedValue);
}
}
You have to add this to your gsonBuilder.
new GsonBuilder().registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY).create();
Answer found on this question Could not serialize object cause of HibernateProxy
Related Topics
Could Not Set the Column Width to Zero I.E. Not Made Column Invisible
Extracting Pairs of Words Using String.Split()
Why Does This Subtraction Not Equal Zero
Java: How to Do Double-Buffering in Swing
Messagebodyprovidernotfoundexception While Running Jar from Command Line
Jtable + Sorting Specific Field
Karate Karate-Config.Js Not a Js Function
How to Change Background Color of Jtabbedpane
Java Equivalent of Unsigned Long Long
What Are Shadow Variables in Java
Cannot Instantiate the Type List<Product>
Swing - Thread.Sleep() Stop Jtextfield.Settext() Working
When Does the Main Thread Stop in Java
Superclass Reference Not Able to Call Subclass Method in Java
Java: Exceptions as Control Flow
Should I Retrieve Database Record in Struts2 View Layer
Converting String to Date Using Simpledateformat Is Returning Random Date