Serializng Hibernate entities into JSON using Jackson framework

Sep 05, 2010 21:38

JSON serialization format is all the rage these days when it comes to making Ajax calls from the browser. And Jackson JSON-processor is probably the best and most popular serializer written in Java for converting domain objects into JSON. Hibernate is, of course, the most popular object-relational mapping framework. And the problem is that those ( Read more... )

java, json, hibernate, jackson

Leave a comment

Comments 33

Works like a charm, but can you elaborate how do I go about limiting the tree graph? ext_550111 May 11 2011, 05:30:24 UTC
This works like a charm! however, it does get me quite big graphs :)

Is there a way to make it work only with already initialized entities and not attempt to initialize anything (or am I wrong and it doesn't)? I'd like the service to decide what to initialize and when
Can it be done? (or did I miss something and you already do that) :)

Thanks again

Reply

Re: Works like a charm, but can you elaborate how do I go about limiting the tree graph? kyrill007 May 11 2011, 13:19:58 UTC
Yes, you missed it all. :-) This object mapper does not load uninitialized Hibernate proxies but stops at them and converts them to JavaScript 'nulls'. So, if you object graph is too big, it is because that's what you're supplying to the mapper. :-)

Reply

Re: Works like a charm, but can you elaborate how do I go about limiting the tree graph? kyrill007 May 11 2011, 13:21:36 UTC
And, of course, I was talking about my code, i.e. the content of the actual post, not the comments with modifications.

Reply

Re: Works like a charm, but can you elaborate how do I go about limiting the tree graph? ext_550111 May 19 2011, 01:01:57 UTC
Yes, a minute after I posted I looked again at the code and realized this is just what you do :) and indeed it works like a charm!

My problem now is with Generics, and I seen somewhere that Jackson 1.8 should take care of it, so I downloaded it but now it broke your code :) it doesn't compile... I tried to do some quick fixes here, but besides passing nulls in new introduced parameters to buildCollectionSerializer and buildMapSerializer I have no clue what to do

Is there a chance you have a 1.8 compatible version by any chance? ;)

Reply


Deserializing objects anonymous June 2 2011, 14:26:22 UTC
Hi, this is good stuff thanks for the post. I'm curious if you have tackled turning json into hibernate pojos.

Reply

Re: Deserializing objects kyrill007 June 2 2011, 14:28:47 UTC
Hibernate pojos are just Java beans. Jackson should be able to transparently populate those. What is the actual issue?

Reply


Infinite recursion problem while fetching the newly saved record anonymous June 10 2011, 08:10:07 UTC
Hi Kyrill. Thanks for posting the solution. We are using spring(3.0.3.RELEASE) + JPA(spring-jap2.0.8) + Hibernate(3.5.4-Final) + Jackson(1.8.1) + Jersey(1.7) in our project.
The lazy load problem while using "mapper.writeValueAsString()" was solved using the above solution.

But the problem we are facing right now is like this -
We use javax enetitymanager for making DB transactions.
We create an entity which has bi-directional associations (using em.persist(entity)). The record gets saved very nicely.
We load the newly saved record immediately, but we get infinite recursion problem :
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)

What could be the way to avoid this problem ??
We understand this is due to bi-directional dependency and we don't want to use @JsonIgnore also.

Reply

Re: Infinite recursion problem while fetching the newly saved record kyrill007 June 10 2011, 12:31:34 UTC
Yes, if you don't want to use @JsonIgnore on one of the ends of a bi-directional association, then you need to write your own JSON serialization framework that is more clever than Jackson. :-)

Reply

Re: Infinite recursion problem while fetching the newly saved record anonymous June 12 2011, 09:17:21 UTC
Thanks Kyrill. Actually, I tried using @JsonBackReference and @JsonManagedReference, and it solved the problem :))
But have another challenge now. The pojos are auto generated using hbm2java goal of hibernate-tools maven plugin with reverse engineering, and that won't generate these custom annotations (may be I am not aware of how to do this.) :(

Reply


u rock anonymous June 11 2011, 21:44:35 UTC
Thanks a lot for the solution ..It worked great!

Reply


Thank you! Super solution. Would like to provide an enhancement suggestion for future :-)! anonymous July 18 2011, 10:05:57 UTC
Thanks Kyrill! This solution did work.

My other problem is that the model POJO's are auto-generated via HBM files as it has been reported by the other user above. So are there any plans in future to make this solution work probably by providing some exclusion rules explicitly on the objectmapper ? instead of the @JsonIgnore annotation? (I have seen some JSONGenerators providing this sort of exclusion rules, so just curious.)

However for now its not a very painful exercise as the list of HBMs are only handful and I can go and modify these POJOs myself. But can see this becoming a bottleneck when the list grows huge and each subsequent code generation could become painful.

Overall your solution is the best I have searched so far! Thank You!

Reply


Leave a comment

Up