spring boot之解决懒加载session was closed问题
2017-08-01 16:25
615 查看
【原创文章,转载请注明出处】
spring与JPA结合时,如何解决懒加载no
session or session was closed!!!
实际上Spring Boot是默认是打开支持sessionview filter的,所以大家正常应该是不会发现有这个问题的,但是还是有人提出了,好吧,如果真的碰到的话,那么可以按照如下尝试解决下。
我们先看看有这么几个类(省略一些代码,只提供核心的):
Teacher:
@Entity
public class Teacher {
@Id @GeneratedValue
private long id;
private String teaName;
}
Student:
@Entity
public class Student {
@Id @GeneratedValue
private long id;
private String stuName;
@ManyToOne(fetch = FetchType.LAZY)
private Teacher classTeacher;
}
StudentRepository:
public interface StudentRepository extends CrudRepository<Student,Long>{
}
访问控制器:
@RequestMapping("/hello")
public Stringhello(Map<String,Object> map){
map.put("student",studentRepository.findOne(1L));
return "/hello";
}
访问/hello那么如果出现如下异常信息:
org.hibernate.LazyInitializationException:
couldnot initialize proxy - no Session
那么可以这是由于我们使用懒加载加载数据的方法,当我们要获取的数据的时候,但是session已经关闭了,我们支持在S
4000
pring MVC中需要配置一个OpenEntityManagerInViewFilter过滤器,Spring针对Hibernate的非JPA实现用的是OpenSessionInViewFilter,那么在Spring
Boot中怎么支持呢?
特别特别的简单,只需要在application.properties中加入如下配置:
spring.jpa.open-in-view=true
这么一个配置即可支持,默认这个值就为true。
spring与JPA结合时,如何解决懒加载no
session or session was closed!!!
实际上Spring Boot是默认是打开支持sessionview filter的,所以大家正常应该是不会发现有这个问题的,但是还是有人提出了,好吧,如果真的碰到的话,那么可以按照如下尝试解决下。
我们先看看有这么几个类(省略一些代码,只提供核心的):
Teacher:
@Entity
public class Teacher {
@Id @GeneratedValue
private long id;
private String teaName;
}
Student:
@Entity
public class Student {
@Id @GeneratedValue
private long id;
private String stuName;
@ManyToOne(fetch = FetchType.LAZY)
private Teacher classTeacher;
}
StudentRepository:
public interface StudentRepository extends CrudRepository<Student,Long>{
}
访问控制器:
@RequestMapping("/hello")
public Stringhello(Map<String,Object> map){
map.put("student",studentRepository.findOne(1L));
return "/hello";
}
访问/hello那么如果出现如下异常信息:
org.hibernate.LazyInitializationException:
couldnot initialize proxy - no Session
那么可以这是由于我们使用懒加载加载数据的方法,当我们要获取的数据的时候,但是session已经关闭了,我们支持在S
4000
pring MVC中需要配置一个OpenEntityManagerInViewFilter过滤器,Spring针对Hibernate的非JPA实现用的是OpenSessionInViewFilter,那么在Spring
Boot中怎么支持呢?
特别特别的简单,只需要在application.properties中加入如下配置:
spring.jpa.open-in-view=true
这么一个配置即可支持,默认这个值就为true。
相关文章推荐
- 使用clone解决hibernate+spring集成中的延迟加载问题及分析(no session or session was closed)
- 使用clone解决hibernate+spring集成中的延迟加载问题及分析(no session or session was closed)
- ThreadLocal的实现原理,及使用实例,解决spring,hibernate非web项目下的懒加载 no session or session was closed(2)!
- ThreadLocal的实现原理,及使用实例,解决spring,hibernate非web项目下的懒加载 no session or session was closed(1)!
- Hibernate延迟加载 或 no session or session was closed 问题的解决(OpenSessionInViewFilter使用)
- failed to lazily initialize a collection of role: no session or session was closed (Hibernate) 解决方法(延迟加载问题)
- 出现org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed问题以及解决办法
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题
- spring.net Could not initialize proxy - the owning Session was closed 解决
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题
- Spring Boot不允许加载iframe问题解决
- 37-002-1 通过spring的OpenSessionInViewFilter解决jsp页面懒加载显示的问题
- 解决spring+springMVC+Hibernate+spring-data-jpa懒加载No session问题
- spring boot 本地图片不能加载(图片路径)的问题及解决方法
- IntelliJ IDEA搭建Springboot项目解决热加载问题
- spring boot--整合spring session实现session共享以及cookie丢失问题解决
- SHH could not initialize proxy - the owning Session was closed 和延迟加载问题
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题
- 解决eclipse创建spring boot项目加载不到application.properties配置文件的问题
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题