When the project is running, an error message is reported as follows:

org.hibernate.LazyInitializationException: could not initialize proxy [xxx.domain.Guild#CF12263C600F4BCABC9293D3FABE4B42] - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169) ~ [hibernate core - 5.3.9. Final. Jar! / : 5.3.9. The Final] the at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309) ~ [hibernate core - 5.3.9. Final. Jar! / : 5.3.9. The Final] the at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) ~ [hibernate core - 5.3.9. Final. Jar! / : 5.3.9. The Final] the at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) ~ [hibernate core - 5.3.9. Final. Jar! / : 5.3.9. The Final] at XXX. Domain. The Guild $HibernateProxy $58 nsae2j. GetName (Unknown Source) ~ [classes! / : 0.0.9 - the SNAPSHOT] at XXX. Task. TaskJiaoFuService. GuildName (TaskJiaoFuService. Java: 181) ~ [classes! / : 0.0.9 - the SNAPSHOT] at XXX. Task. TaskJiaoFuService. Result2JiaoFuDetail (TaskJiaoFuService. Java: 122) ~ [classes! / : 0.0.9 - the SNAPSHOT] at XXX. Task. TaskJiaoFuService. ParseResult (TaskJiaoFuService. Java: 106) ~ [classes! / : 0.0.9 - the SNAPSHOT] at XXX. Task. TaskJiaoFuService. QueryV4 (TaskJiaoFuService. Java: 91) ~ [classes! / : 0.0.9 - the SNAPSHOT] At XXX. AbstractExportStrategy. Query (AbstractExportStrategy. Java: 65) ~ [classes! / : 0.0.9 - the SNAPSHOT] the at XXX. ExportService. ExportAndPersistence (ExportService. Java: 130) [classes! / : 0.0.9 - the SNAPSHOT] the at XXX. ExportService. Lambda $execute $0 (ExportService. Java: 75) [classes! / : 0.0.9 - the SNAPSHOT] the at xxx.common.initialization.ContextCopyingTaskDecorator.lambda$decorate$0(ContextCopyingTaskDecorator.java:20) ~ [classes! / : 0.0.9 - the SNAPSHOT] at Java. Util. Concurrent. ThreadPoolExecutor. RunWorker (ThreadPoolExecutor. Java: 1149) ~ [na: 1.8.0 comes with _181] at Java. Util. Concurrent. ThreadPoolExecutor $Worker. The run (ThreadPoolExecutor. Java: 624) ~ [na: 1.8.0 comes with _181] the at Java. Lang. Thread. The run (Thread. Java: 748) ~ [na: 1.8.0 comes with _181]Copy the code

The business is very simple, a jPA single table query, get attribute error

Analysis of the

JPA uses lazy loading by default. Even when accessing a single entity class, the object returned is a proxy, and database queries are performed when obtaining object properties. Session at this time if the connection data released will throw the exception org. Hibernate. LazyInitializationException in classmates often use hibernate or jpa may often encountered, search on the Internet, there are many kinds of, the way to solve the problem Here’s a list:

  • Add spring.jpa.open-in-view=true to the spring Boot configuration file application.properties
  • With the spring OpenSessionInViewFilter
  • In the spring of the boot configuration file the application. The properties to add spring. Jpa. Properties. Hibernate. Enable_lazy_load_no_trans = true
  • Add @proxy to the offending entity class (lazy = false)

spring.jpa.open-in-view

Let’s take a look at baeldung, portal :www.baeldung.com/spring-open…

Session per request is a transactional pattern to tie the persistence session and request life-cycles together. Not surprisingly, Spring comes with its own implementation of this pattern, named OpenSessionInViewInterceptor, to facilitate working with lazy associations and therefore, improving developer productivity.

……

By default, OSIV is active in Spring Boot applications. Despite that, as of Spring Boot 2.0 it warns us of the fact that it’s enabled at application startup if we haven’t configured it explicitly:

spring.jpa.open-in-view is enabled by default. Therefore, database 
queries may be performed during view rendering.Explicitly configure 
spring.jpa.open-in-view to disable this warning
Copy the code

Each request session is a transaction mode for Spring, so we enable it by default to improve development efficiency, but if you do not explicitly configure it, I will give you a warning. There’s a lot of debate about this default configuration on Github: github.com/spring-proj… The sequence diagram of OSIV is as follows:

In the project, spring.jpa.open-in-view is set to false, and the code fetching entity classes or associated entities is done in the service. Generally, we enable transactions, and there is no problem fetching lazy data in the context of transactions. And open data after the session will wait until after completion of the entire request will release, is actually very consumption performance, with other students without shut down before the open – in – the view problems: www.cnblogs.com/thisismarc/…

The OpenSessionInViewFilter configuration is also passed. The OpenSessionInViewFilter configuration is not recommended

spring.jpa.properties.hibernate.enable_lazy_load_no_trans

We also see baeldung how said, portal: www.baeldung.com/hibernate-l… While using lazy loading in Hibernate, we might face exceptions, saying there is no session. The recommended approach is to design our application to ensure that data retrieval happens in a single transaction. But, this can sometimes be difficult when using a lazy entity in another part of the code that is unable to determine what has or hasn’t been loaded.

Hibernate has a workaround, an enable_lazy_load_no_trans property. Turning this on means that each fetch of a lazy entity will open a temporary session and run inside a separate transaction.

This is a workaround to open a temporary session for each lazily loaded entity, but it is also anti-human because if more lazily loaded associated entities are requested, more additional connections will be requested, which will strain the database connection. Each association loaded in a new transaction forces the transaction log to be refreshed after each association initialization, so it is strongly discouraged.

@Proxy(lazy = false)

@proxy (lazy = false) means similar to FetchType.EAGER, and returns an initialized entity, which turns lazy loading off, which is definitely not what we want

Recommended solutions

Transactional transactions are explicitly annotated. Query interfaces do not normally have @Transactional annotations configured. The solution is to implement the @Transactional(readOnly = True) annotation on Service query methods to separate transaction boundaries. Adding related methods to the scope of transaction control does not cause this problem. If there is a better way, please leave a comment

Refer to the link

Vladmihalcea.com/the-hiberna… www.baeldung.com/spring-open… Github.com/spring-proj… www.cnblogs.com/thisismarc/… www.baeldung.com/hibernate-l…