报错org.hibernate.LazyInitializationException: could not initialize proxy - no Session,差不多都是因为懒加载异常,

报错截图:

解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session报错-懒加载问题

大概意思就是初始化的时候session关闭了,这个主要是因为hibernate默认的懒加载策略:默认lazy为true 引起的异常。

解决方案:

以下方案选其一即可。

1、如果你有xml配置文件,可以在class标签里,加一个lazy="true",将lazy设成false

2、如果你使用的是springboot的jpa,可以在配置文件里加一个配置:(推荐使用)

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session报错-懒加载问题

3、使用OpenSessionInViewFilter解决解决懒加载问题,在web.xml中配置,放到struts过滤器之前

<!-- 配置Spring的OpenSessionInViewFilter,以解决懒加载问题  -->
<filter>
	<filter-name>OpenSessionInViewFilter</filter-name>
	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>OpenSessionInViewFilter</filter-name>
	<url-pattern>*.action</url-pattern>
</filter-mapping>

我使用的是springboot的jpa报的这个错,加了一行配置就搞定了,希望可以帮到你。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-03
  • 2021-10-20
  • 2021-04-27
  • 2022-03-07
  • 2022-02-21
  • 2021-08-19
猜你喜欢
  • 2021-06-02
  • 2021-12-09
  • 2021-06-14
  • 2021-07-20
  • 2022-12-23
相关资源
相似解决方案