Spring JPA Hibernate problem: LazyInitializationException: could not initialize proxy no session
2016-01-03 22:45
597 查看
在Spring MVC中使用Spring JPA的时候出现这个错误:LazyInitializationException: could not initialize proxy no session,我的用法是在class中用到了fetch=FetchType.LAZY,这个错误的原因是说:当尝试去取Lazy load的属性时却发现Hibernate Session已经关闭,所以报这个错误。我是使用的Spring data,因此不需要直接操作hibernate相关class. 更详细的解释参见:http://stackoverflow.com/questions/16773506/understanding-spring-mvc-configuration
and http://www.codedata.com.tw/java/hibernate-lazy-loading/ 。
我的处理方式是:在srping mvc配置文件中多配置一个filter, 在此filter的生命周期中打开hibernate session。如下:
<!-- for JPA Hibernate problem: LazyInitializationException: could not initialize proxy no session,
see: http://stackoverflow.com/questions/16773506/understanding-spring-mvc-configuration and http://www.codedata.com.tw/java/hibernate-lazy-loading/ entityManagerFactory is defined in ApplicationContext.xml
Note: if no this hibernate interceptor, lazy load object in Controller will fail -->
<mvc:interceptors>
<bean class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
</property>
</bean>
</mvc:interceptors>
这样做在Controller中的lazy load是没错误了,但是如果在其他的模块中调用还可能出错,例如:我在spring security中的UserDetailsService的实现类中报错了,因此我添加了 @Transactional注解。
// if not add @Transactional, all httpFilter call this method will fail. could not initialize proxy - no Session
// see also: http://stackoverflow.com/questions/32603250/could-not-write-json-could-not-initialize-proxy-no-session-error @Transactional(readOnly=true)
public UserDetails loadUserByUsername(String email)
and http://www.codedata.com.tw/java/hibernate-lazy-loading/ 。
我的处理方式是:在srping mvc配置文件中多配置一个filter, 在此filter的生命周期中打开hibernate session。如下:
<!-- for JPA Hibernate problem: LazyInitializationException: could not initialize proxy no session,
see: http://stackoverflow.com/questions/16773506/understanding-spring-mvc-configuration and http://www.codedata.com.tw/java/hibernate-lazy-loading/ entityManagerFactory is defined in ApplicationContext.xml
Note: if no this hibernate interceptor, lazy load object in Controller will fail -->
<mvc:interceptors>
<bean class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
</property>
</bean>
</mvc:interceptors>
这样做在Controller中的lazy load是没错误了,但是如果在其他的模块中调用还可能出错,例如:我在spring security中的UserDetailsService的实现类中报错了,因此我添加了 @Transactional注解。
// if not add @Transactional, all httpFilter call this method will fail. could not initialize proxy - no Session
// see also: http://stackoverflow.com/questions/32603250/could-not-write-json-could-not-initialize-proxy-no-session-error @Transactional(readOnly=true)
public UserDetails loadUserByUsername(String email)
相关文章推荐
- Spring整合Dwr,Cannot locate BeanDefinitionParser for element [annotation-scan]
- spring3 MVC手工搭建Spring3项目demo
- spring mvc 开发需要的jar包的用途详解
- 基于注解的 Spring MVC 简单入门-网摘
- [MVC] 深入浅出Spring MVC
- Spring MVC 为例,从前台—>后台—>前台的编解码过程及出现乱码的案例
- (struts2) spring mvc + mybatis + maven整合
- Spring+Spring MVC 整合JasperReports
- 使用springmvc框架之第1篇:spring中bean的系统自动注册
- SpringMVC Json
- ajax跨域:JQuery + Spring MVC3.0
- spring mvc详解1
- Spring MVC和Struts2的区别
- Grails,移动互联网时代服务器端开发的利器
- Spring MVC on JBoss 7.1 final
- Spring MVC 框架搭建及详解
- Spring MVC 教程,快速入门,深入分析
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SpringMVC 集成JSR303数据校验
- Spring MVC+hibernate+Spring的框架搭建中遇到的问题总结