"Bug record" JPA resolution Org.hibernate.lazyinitializationexception could not initialize proxy-no session

Source: Internet
Author: User


Recent development projects are busy, Springcloud notes to a little put, the afternoon out of a bug, disgusting, the function is very simple, there is no cascade or complex mapping relationship, is always reported three exceptions

Caused by:com.Fasterxml.Jackson.DataBind.jsonmappingexception: No Serializer found for classOrg.Hibernate.Proxy.Pojo.javassist.JavassistlazyinitializerAnd no properties discovered to createBeanserializer(To avoid exception, disable serializationfeature.Fail_on_empty_beans) (through reference chain:com.Shunneng.Core.Web.Rest.util.Result["Data"]->com.Shunneng.Flup.Web.Rest.DTO.Saveflupdto["Flupform"]->com.Shunneng.Flup.Domain.flupform_$$_jvst158_3["Handler"])
2018-05-14   : 54 : 44 , 571  ERROR Xnio -2  task-3  c.s . c . w . r . e . exceptiontranslator : 159 -jsonmappingexception:no serializer found for  class  org.hibernate . proxy . pojo . javassist . javassistlazyinitializer  and no properties discovered to create  (To avoid exception, disable Serializationfeature.) (through reference chain:  
org.hibernate.lazyinitializationexception could not initialize proxy - no session

These anomalies are the final result of this anomaly, the Internet to find a lot, are said to be jpa或hibernate的延迟加载 engaged in ghosts, the solution is nothing more than a few, respectively, put together with you to share

Body Five Solutions 1, close LazyInitializationWill fetchSet into eager, can be in the configuration file, can also be annotated 2, the spring bootThe configuration file application.propertiesAdd to spring.jpa.open-in-view=true, yml the same 3, with spring OpenSessionInViewFilter4, in spring bootThe configuration file application.propertiesAdd to spring.jpa.properties.hibernate.enable_lazy_load_no_trans=trueThe above several, because uses is the Springboot project, only uses the annotation to be convenient, has not succeeded, on the unknown written. Introduction to the next method, I was found on a foreign site 5, in the case of the entity class on the problem add @Proxy(lazy = false)Here copy under the machine turn, English good can see

There are situations when we want a simple solution that doesn't care about anything in terms of performance, for example for testing or prototyping purposes. In this case, you can make the following configuration changes in the application to avoid this error, but keep in mind that if this code enters the production environment, pre-initialization may cause the impact.

If you are using XML configuration: default-lazy =“false” Add to your component
If you use annotation configuration: It will be @Proxy(lazy = false) added to all entity classes.

This is how to fix the exception in thread "main" org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话 . As we've seen, this error mainly occurs when you close the connection and try to access a proxy object that is not fully initialized. Because the proxy object requires a connection, you can reconnect the object to the session, or be careful to avoid writing code that accesses the uninitialized proxy object.

LazyInitializationExceptionanother way to avoid this is lazy =“false” to disable the lazy initialization of hibernate for an entity class by using it or to default-lazy =“false” disable it entirely by using it.

For performance reasons, this solution is not recommended for production purposes, but can be used for prototyping, testing, and demonstration. When you upgrade from Hibernate 2.1 to 3.0, don't be surprised if you see this error for the first time, because this is the version that hibernate defaults to when lazy initialization is enabled. If you encounter this error or try to resolve it in any other scenario “org.hibernate.LazyInitializationException:无法初始化代理 - 无会话” , you can also publish the error and code here, and we can take a look at it together.

Cited article: Dealing with Org.hibernate.LazyInitializationException:could not initialize Proxy-no Session in Hibernate Java

This article only makes records, writes the text to help more people, reprint also hoped to indicate the source http://www.cnblogs.com/hellxz/p/9037597.html

"Bug record" JPA resolution Org.hibernate.lazyinitializationexception could not initialize proxy-no session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.