springboot,hibernate,exception no session 问题解决

简介:     异常: could not initialize proxy - no Session; Hibernate: select applicatio0_.api_application_id as api_appl1_0_, applicatio0_.

 

 

异常: could not initialize proxy - no Session;

Hibernate: select applicatio0_.api_application_id as api_appl1_0_, applicatio0_.application_description as applicat2_0_, applicatio0_.application_is_service as applicat3_0_, applicatio0_.application_key as applicat4_0_, applicatio0_.application_name as applicat5_0_, applicatio0_.application_secret as applicat6_0_, applicatio0_.application_type as applicat7_0_, applicatio0_.developer_id as develope8_0_, applicatio0_.developer_name as develope9_0_, applicatio0_.service_encoder as service10_0_, applicatio0_.insert_time as insert_11_0_, applicatio0_.last_update_time as last_up12_0_, applicatio0_.service_need_sign as service13_0_, applicatio0_.api_service_id as api_ser14_0_, applicatio0_.service_router_url as service15_0_, applicatio0_.service_status as service16_0_, applicatio0_.service_vm_ips as service17_0_ from api_application applicatio0_ where 1=1 limit ?

2017-12-25 17:52:07.582  WARN 3992 --- [nio-8085-exec-8]
 .w.s.m.s.DefaultHandlerExceptionResolver : 
Failed to write HTTP message: 
org.springframework.http.converter.HttpMessageNotWritableException: 
Could not write JSON: 
failed to lazily initialize a collection of role:
 com.curiousby.baoyou.cn.showandshare.application.developmanage.entity.po.Application.applicationInterfaceList, 
could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
failed to lazily initialize a collection of role: com.curiousby.baoyou.cn.showandshare.application.developmanage.entity.po.Application.applicationInterfaceList,
 could not initialize proxy - no Session (through reference chain: com.curiousby.baoyou.cn.showandshare.application.developmanage.hibernate.PageFinder["data"]->java.util.ArrayList[0]->com.curiousby.baoyou.cn.showandshare.application.developmanage.entity.po.Application["applicationInterfaceList"])

2017-12-25 17:52:07.582  WARN 3992 --- [nio-8085-exec-8] 
.w.s.m.s.DefaultHandlerExceptionResolver : 
Resolved exception caused by Handler execution: 
org.springframework.http.converter.HttpMessageNotWritableException:
 Could not write JSON: failed to lazily initialize a collection of role:
 com.curiousby.baoyou.cn.showandshare.application.developmanage.entity.po.Application.applicationInterfaceList,
 could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
failed to lazily initialize a collection of role: 
com.curiousby.baoyou.cn.showandshare.application.developmanage.entity.po.Application.applicationInterfaceList,
 could not initialize proxy - no Session 
(through reference chain: 
com.curiousby.baoyou.cn.showandshare.application.developmanage.hibernate.PageFinder["data"]->java.util.ArrayList[0]->com.curiousby.baoyou.cn.showandshare.application.developmanage.entity.po.Application["applicationInterfaceList"])

 

解决方法:

1.

JPA Hibernate禁止懒加载

 

2.

在properties文件中添加  配置信息
spring.session.store-type=none

 

3.

在application.properties中加入如下配置:

spring.jpa.open-in-view=true

 

4.

@ManyToOne(fetch = FetchType.LAZY)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!

 

 

目录
相关文章
|
6月前
|
缓存 前端开发 Java
【二十八】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能
【二十八】springboot之通过threadLocal+参数解析器实现同session一样保存当前登录信息的功能
169 1
|
6月前
|
XML Java 数据库连接
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
Spring Boot的数据访问之Spring Data JPA以及Hibernate的实战(超详细 附源码)
113 0
|
1月前
|
前端开发 Java 数据安全/隐私保护
用户登录前后端开发(一个简单完整的小项目)——SpringBoot与session验证(带前后端源码)全方位全流程超详细教程
文章通过一个简单的SpringBoot项目,详细介绍了前后端如何实现用户登录功能,包括前端登录页面的创建、后端登录逻辑的处理、使用session验证用户身份以及获取已登录用户信息的方法。
185 2
用户登录前后端开发(一个简单完整的小项目)——SpringBoot与session验证(带前后端源码)全方位全流程超详细教程
|
1月前
|
缓存 Java Spring
servlet和SpringBoot两种方式分别获取Cookie和Session方式比较(带源码) —— 图文并茂 两种方式获取Header
文章比较了在Servlet和Spring Boot中获取Cookie、Session和Header的方法,并提供了相应的代码实例,展示了两种方式在实际应用中的异同。
158 3
servlet和SpringBoot两种方式分别获取Cookie和Session方式比较(带源码) —— 图文并茂 两种方式获取Header
|
1月前
|
SQL Java 数据库连接
springBoot+Jpa(hibernate)数据库基本操作
springBoot+Jpa(hibernate)数据库基本操作
42 0
|
3月前
|
缓存 安全 Java
|
3月前
|
缓存 安全 Java
Hibernate 中的 Session 是线程安全的吗?
【8月更文挑战第21天】
43 0
|
3月前
|
缓存 安全 Java
Hibernate 中的 Session 是什么?
【8月更文挑战第21天】
42 0
|
3月前
|
Java 关系型数据库 MySQL
|
4月前
|
Java 数据库连接 数据库
如何在Spring Boot中集成Hibernate
如何在Spring Boot中集成Hibernate