Failed to initialize proxy - no session

Good afternoon, I spent a lot of time on Googlin and switched from JSON 1 to JSON 2, but when I try to return a list of entities, I get an exception:

"Exception received Failed to write JSON: failed to initialize proxy - no session (via chain of links:"

I know that this is due to the lazy initialization of Hibernate, but I did not understand how to fix it. I thought the jackson-hibernation solution is a solution. However, I could not get it to work.

Here is my technique:

  • 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); } } 

A thread starts with a controller that calls several services, each of which returns an object or list. Then I put the returned objects in the object and return it, thinking that @ResponseBody will work. However, I should not have things connected correctly, as I am still getting exceptions.

Can anyone see any errors here?

thankful ...

+4
source share
2 answers

This is allowed in version 2.5.0 of the jackson-datatype-hibernate type. Just pass the Hibernate SessionFactory in the Hibernate (3,4) module as follows:

 public class HibernateAwareObjectMapper extends ObjectMapper { private static final long serialVersionUID = 1L; @Autowired private EntityManagerFactory emf; @PostConstruct public void init() { Hibernate4Module module = new Hibernate4Module(emf.unwrap(SessionFactory.class)); module.enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING); module.disable(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION); }} 

Above snippet is fine if JPA is used, therefore EntityManagerFactory bean is available. Otherwise, just enter the Hibernate SessionFactory and go to the constructor.

No need to use the OpenSessionInView template.

+3
source

This is because the object you are trying to serialize has a lazy loaded component, you can add OpenSessionInViewFilter to your web configuration to fix this problem.

The hibernation session used to load the object is closed during the transaction, therefore, when the view level (jackson) tries to serialize the object and tries to load the lazy loaded objects, it does not work with this error.

Add this to your web.xml

 <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/app/*</url-pattern> </filter-mapping> 
+1
source

Source: https://habr.com/ru/post/1469409/


All Articles