本文主要是为了解决“spring4+springmvc+springdataJPA+hibernate4整合”,注解了OneToMany、ManyToOne,并配置了懒加载,然后报错的问题
org.hibernate.LazyInitializationException: could not initialize proxy - no Session at
在web.xml中,配置springSevlet的位置,使用org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter,代码以下:
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ......., no session or session was closed
1般是在定时任务quartzJob中,或Junit测试中使用懒加载出现的毛病
添加事务配置注解。junit代码以下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/applicationContext.xml",
"file:src/main/webapp/WEB-INF/demo-servlet.xml"})
@Transactional
public class Test {
终了。
上一篇 上网页面被强制广告――简单分析
下一篇 Antlr v4入门教程和实例