FasterXML / jackson-datatype-hibernate

Add-on module for Jackson JSON processor which handles Hibernate (https://www.hibernate.org/) datatypes; and specifically aspects of lazy-loading
317 stars 106 forks source link

Incorrect serialization of Lazy objects #77

Open EddieJamsession opened 8 years ago

EddieJamsession commented 8 years ago

More you can find here:

http://stackoverflow.com/questions/33194554/two-different-resulting-jsons-when-serializing-lazy-objects-and-simple-objects

Guys, I've already find out, what's the problem and solved it with hack, when we try to serialize Hibernate proxy, and isUnwrappingSerializer() is being called, the HibernateProxySerializer is always returning false as per parent class.

But it should return the same answer that the actual serializer of the proxied value object is returning. And now we have problem that unwrapping instructions are just ignored.

cowtowncoder commented 8 years ago

At this point, a unit test would be needed.

cowtowncoder commented 8 years ago

I don't doubt there is an issue here; but a full reproduction would be useful to do proper fix. Problem is probably bigger than just relaying of isUnwrappingSerializer(), as there should also be delegation for other methods, and it would be great to fix other issues that stem from this.

alanhay commented 7 years ago

For information and as the original SO question has been deleted,I have encountered the same issue as reported here:

http://stackoverflow.com/questions/42395831/issue-serializing-lazy-manytoone

Any workround? If any further information would be useful please advise.

shakuzen commented 7 years ago

I don't know if my situation is exactly the same as the other two people reporting here, but I am noticing the same thing. I have made a repository that reproduces the issue in a JUnit test: https://github.com/shakuzen/jackson-hibernate-serialization-repro

@cowtowncoder I have done a lot of debugging to understand what is going on here (at least in my case). Spring Data REST's PersistentEntityJackson2Module$NestedEntitySerializer will wrap entities in a org.springframework.hateoas.Resource object. When the entity is actually a HibernateProxy, serialization will be performed by this module's HibernateProxySerializer, but since it is not an UnwrappingSerializer, the Resource's content field name will be serialized whereas it is expected to be unwrapped since it is marked with @JsonUnwrapped. This is why the result has a content field that does not belong, with the expected entity as its value.

I'm not sure exactly what should be changed in what way, which is why I didn't just open a pull request, but let me know if I can help with anything.

sullrich84 commented 5 years ago

Any updates on this issue?

sdwarwick commented 5 years ago

Although this appears to be an old problem, it has far-reaching implications for system optimization. In cases where @Entities are being used both to specify database and JSON serialization strategies, it is not possible to optimize fetching strategies in JPA/Hibernate in production without impacting serialization since @JsonUnwrapped is not respected by this module. In our case, we have close to 100 JSON/XML Spring @Responsebody endpoints that will be impacted by moving to lazy fetch - either by unintentionally restructuring the JSON response or failing due to the lazy load issue.

angheladrian commented 5 years ago

We have the same problem. Any updates or plans?