jackson-datatype-hibernate icon indicating copy to clipboard operation
jackson-datatype-hibernate copied to clipboard

Incorrect serialization of Lazy objects

Open EddieJamsession opened this issue 9 years ago • 7 comments

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.

EddieJamsession avatar Oct 18 '15 12:10 EddieJamsession

At this point, a unit test would be needed.

cowtowncoder avatar Dec 27 '15 04:12 cowtowncoder

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.

cowtowncoder avatar Jan 04 '16 01:01 cowtowncoder

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.

alanhay avatar Feb 22 '17 18:02 alanhay

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.

shakuzen avatar Apr 03 '17 10:04 shakuzen

Any updates on this issue?

sullrich84 avatar Dec 04 '18 22:12 sullrich84

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.

sdwarwick avatar Jan 15 '19 15:01 sdwarwick

We have the same problem. Any updates or plans?

angheladrian avatar Jul 29 '19 20:07 angheladrian