We have an existing DB and we started creating Entites for it using hibernate in Java. we are building a web application using play! framework so we are using Jackson to serialize and deserialize our objects to and from the client. In one function we query the DB for a list of Entities (Report for example) and the problem is that some attributes are being Lazy fetched even though we explicitly specified an Eager fetch, which causes a problem in serialization using jackson, because it doesnt know how to handle Lazy fetched objects.
Exception:
No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: xxxxxxxx)
Explaination:
//Our current situation is:
//We have a class report and a class user
Class Reprot{
[join and fetch eager]
User user;
}
Class User{
String firstName;
String lastName;
}
//And we query a list of Reports:
List<Report> list = typedQuery.getResultList();
//When we try to serialze it:
JsonNode jn = Json.toJson(list);
//it throws the above exception
From what we found, the problem is the fact that hibernate uses a cache (first-level cache) so it wont have to fetch things from the DB again and again, and it is probably what it does because each User can have more than one Reports. So what it does is query the list, and when it sees that the current report's user has already been fetched from the DB it puts a proxy instead of an instance, and that proxy is being treated as a Lazy object which can't be serialized.
We searched for a solution and we cant find it, hoping anyone here can help us.