解决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

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报的这个错,加了一行配置就搞定了,希望可以帮到你。

 


版权声明:本文为Lin_xiaofeng原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。