标签:springdata hibernate4 onetomany 懒加载
本文主要是为了解决“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
一般是在定时任务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 {
完毕。
spring4+springmvc+springdataJPA+hibernate4整合懒加载问题
标签:springdata hibernate4 onetomany 懒加载
原文地址:http://blog.csdn.net/cyzshenzhen/article/details/45420513