Showing posts with label Hibernate4Module. Show all posts
Showing posts with label Hibernate4Module. Show all posts

Monday, January 25, 2016

Appropirate Usage of JacksonDatatypeHibernate

Jackson, Spring and Hibernate are the three popular frameworks in their respective technologies but there are some issues occuring while using all these 3 together due to serialization of lazy loaded hibernate objects.

I faced a similar issue while converting Hibernate object to JSON using Jackson API in a Spring based web application and following is a sample error description:

No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer  to avoid exception, disable 
SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS)



Approach to solve the above issue:
Having gone through various forums and many different solutions, I have decided to zero in on using JacksonDatatypeHibernate add-on to fix the lazy loading concerns.

Step 1:
Write an ObjectMapper extension-class in one of your packages to inject Hibernate4Module object into ObjectMapper:




Step 2:
Register ObjectMapper extension-class(HibernateAwareObjectMapper) in the Spring-Config.xml with the following configuration:

Important note:
Never configure message converters outside of the annotation-driver tag when mvc:annotation-driven is enabled.  The registered message converter has to be inside the message-converters tag.

This is it.

How J2EE components work together in any Container - Spring or Application Server

In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for B...