Good day all, I have spent considerable time Googlin' and upgrading from JSON 1 to JSON 2 but when I try and return a list of entities, I get the exception:
"Received Exception Could not write JSON: could not initialize proxy - no Session (through reference chain:"
I know this is related to Hibernate lazy init but I haven't figured out how to fix it. I thought the jackson-module-hibernate project was the solution. However I haven't been able to make it work.
Here is my tech:
- Spring: 3.2.1RELEASE
- Hibernate 4.1.7.Final
- Jackson 2
- jackson-core 2.0.4
- jackson-databind: 2.0.4
- jackson-datatype-hibernate4: 2.1.2
I am using Java Config: ...
@Bean
public com.mycompany.config.MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setObjectMapper(new HibernateAwareObjectMapper());
return mappingJackson2HttpMessageConverter;
}
@Bean
public OpenEntityManagerInViewFilter springOpenEntityManagerInViewFilter() {
return new org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter();
}
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
final ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
contentNegotiatingViewResolver.setDefaultContentType(MediaType.APPLICATION_JSON);
final ArrayList defaultViews = new ArrayList();
defaultViews.add(converter());
contentNegotiatingViewResolver.setDefaultViews(defaultViews);
return contentNegotiatingViewResolver;
}
I also have:
public class HibernateAwareObjectMapper extends ObjectMapper {
public HibernateAwareObjectMapper() {
Hibernate4Module hm = new Hibernate4Module();
registerModule(hm);
}
}
The flow starts from the controller, which calls several services that each return an entity or List. I then put the returned objects into a object and return it, thinking the @ResponseBody will work. However, I must not have things wired right since I still get the exceptions.
Can anyone see any errors here?
Gratefully...