1

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.

  • Try jackson hibernate module. https://github.com/FasterXML/jackson-datatype-hibernate – mkrakhin Dec 09 '14 at 13:48
  • You may *think* you have instances of your objects, but what you have are java.reflect.Proxy instances which can't be serialized easily. This was one of the facepalm moments that made me ditch hibernate. It's funny that you tagged the question with jackson. I also had jackson on my project for an external web interface. I realized, I already have one way of serializing objects (JSON), I don't need another one (sql database). So I just switched to Mongo and used my Jackson serializers for persistence serialization as well. I have no idea of how many thousand lines of code I threw away. – aioobe Dec 09 '14 at 13:49
  • Check if this helps http://stackoverflow.com/a/24994562/3447216. – Priyesh Dec 09 '14 at 13:55

1 Answers1

0

You can handle this situation in a couple of ways. I know all the solutions are tough and time consuming.

  1. Do not send the entities to the client directly. Instead create a copy(pojo) of the entites > map the entity values to these newly created pojos > return it to the client. There are a bunch or libraries that can help you map similar objects automatically. Search in internet.

  2. Add a Jackson @Jsonignore annotation to ignore the property which you don't want.

Zeus
  • 6,386
  • 6
  • 54
  • 89