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

having some problems anonymous October 6 2010, 14:12:26 UTC
Hi,

I tried your approach handling.

First the the class HibernateAwareSerializerFactory won't compile for me. I had either to change the two line containing config.introspect(type.getRawClass()); to config.introspectClassAnnotations(type.getRawClass()); or to config.introspect(type);

Then the class get's compiled without any errors. I tried to use the Mapper in my spring app using:

However I'm still running in:
org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role ...

Any ideas?

regards,
Frank

Reply

Re: having some problems kyrill007 October 6 2010, 14:27:20 UTC
Right, I forgot to mention that the code was tested on 1.5.x version of Jackson. It looks like he changed the 'SerializationConfig#introspect' method signature in 1.6.0. The code obviously goes pretty deeply into Jackson internals, so I am not surprised that something may have changed internally there to cause lazy initialization exceptions. Give it a shot on 1.5.x and see if it works... It should. If not, I'd like to see your Hibernate mapping.

Reply

Re: having some problems anonymous October 6 2010, 14:48:36 UTC
Hi,

wow that was fast :-). Yes you are right, using the 1.5.7er version it compiles without any failures. However I'm getting the same failure concerning lazy loading. The HibernateAwareObjectMapper constructor get's called during startup, but not the HibernateAwareSerializerFactory during a request. You can find my spring config here: http://pastebin.com/k8n86ABe.

thanks for you help,
Frank

Reply

Re: having some problems kyrill007 October 6 2010, 14:58:02 UTC
I am concerned there with multiple MappingJacksonHttpMessageConverter's in your Spring config. One has HibernateAwareObjectMapper wired in, the other one does not. It does seem like a Spring misconfiguration. I looked at the Spring's source code for MappingJacksonHttpMessageConverter, and it looks solid; once correct object mapper is supplied, everything should work.

Reply


Thanks anonymous November 10 2010, 18:05:28 UTC
Thanks, you saved our day :-)

Reply


Jackson-module-hibernate anonymous February 15 2011, 20:01:52 UTC
Quick note: there is now new Jackson module at: https://github.com/FasterXML/jackson-module-hibernate which is built using this article's ideas and Kirill's help.

Reply


Review Bebbled Review anonymous March 9 2011, 03:11:51 UTC
At this point, there is very little to recommend Gmail Backup over Gmail Keeper, except this: Gmail Backup is free. If basic functionality is all you need, there's no sense in paying for features you won't use; Gmail Keeper's $30 price tag is well out of "impulse buy" range for a narrowly focused utility. Gmail Backup's open-source nature may be a valuable plus for some readers. If development does begin to pick up as promised, it may well add in more functionality over time.

Reply


Exclude those getMethods whose Field is marked Transient with transient java keyword anonymous March 31 2011, 07:21:43 UTC
Hi,

I have modified filterBeanProperties to exclude those getMethods whose Field is marked Transient with Transient java keyword.

/**
* The purpose of this method is to filter out {@link Transient} properties of the bean
* from JSON rendering.
*/
@Override
protected List filterBeanProperties(SerializationConfig config,
BasicBeanDescription beanDesc,
List props) {

//filter out standard properties (e.g. those marked with @JsonIgnore)
props = super.filterBeanProperties(config, beanDesc, props);

filterInstrumentedBeanProperties(beanDesc, props);

//now filter out the @Transient ones as they may trigger "lazy" exceptions by
//referencing non-initialized properties
List transientOnes = new ArrayList //BeanUtils and AnnotationUtils are utility methods that come from ( ... )

Reply


Leave a comment

Up