使用hibernate从一方获取多方信息时报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
引起原因:hibernate加载关联对象的方式有懒加载方式和立即加载方式。 如果在多对一的配置中没有指定加载方式,而一对多的配置中指定了懒加载方式,因此在获取一方是可获取到值,而获取多方时session已经关闭,这时候获取不到多方信息,因此报错。
解决方法:将一对多的加载方式改为立即加载,将多对一的加载改为懒加载。
将一对多的注解配置改为如下所示:
@OneToMany(mappedBy="topicEntity",fetch = FetchType.EAGER)
public List<AnswerEntity> getAnswerList() {
return answerList;
}
public void setAnswerList(List<AnswerEntity> answerList) {
this.answerList = answerList;
}
将多对一的注解配置改为如下所示:
@ManyToOne(fetch=FetchType.LAZY,optional=false)
@JoinColumn(name="topicId")
public TopicEntity getTopicEntity() {
return topicEntity;
}
public void setTopicEntity(TopicEntity topicEntity) {
this.topicEntity = topicEntity;
}
使用hibernate从一方获取多方信息时报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role的更多相关文章
- ssh框架错误:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role。
在做ssh项目练习的时候出现问题: org.hibernate.LazyInitializationException: failed to lazily initialize a collectio ...
- hibernate延迟加载org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not initialize proxy - no Session
public static void main(String[] args) { DeptEntity dept = getDept("402882e762ae888d0162ae888e ...
- Hibernate加载数据失败failed to lazily initialize a collection of role
在测试获取数据库中的数据或者在页面获取时,有时会遇到这样的错误提示: failed to lazily initialize a collection of role: com.exam.entity ...
- FetchType.LAZY 时属性加上@JsonIgnore,避免返回时报错:Could not write JSON: failed to lazily initialize a collection of role
[示例] @OneToMany(fetch=FetchType.LAZY) @JsonIgnore @Fetch(FetchMode.SELECT) @Cascade(value={CascadeTy ...
- 【转】hibernate懒加载的问题,failed to lazily initialize a collection of role
hibernate懒加载的问题,failed to lazily initialize a collection of role hibernate懒加载的问题,failed to lazily in ...
- org.hibernate.LazyInitializationException: failed to lazily initialize
今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的s ...
- 问题:org.hibernate.LazyInitializationException: failed to lazily initialize
今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的s ...
- UI Automator Viewer获取手机镜像时报错
使用UI Automator Viewer获取手机镜像时报错,具体信息如下: Error while obtaining UI hierarchy XML file: com.android.ddml ...
- shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.SerializeUtils.deserialize(SerializeUtils.java:41) ~[shiro-redis-2.4.2.1-RELEASE.jar:na]
shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.Serial ...
随机推荐
- CSS3实现图片循环旋转
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- windows上安装Anaconda和python
下载并安装 anaconda 先到https://www.continuum.io/downloads 下载anaconda, 现在的版本有python2.7版本和python3.5版本,下载好对应版 ...
- 【python】理解循环:for,while
先看下for结构: #!/usr/bin/python # -*- Coding:UTF-8 -*- for i in range(1): print i 输出: 0 输入和输出: #!/usr/bi ...
- HA状态下防火墙损坏处理
问题描述: web登录防火墙管理地址,发现在 状态-系统信息 里集群成员只有一台原备机.到机房发现原主机只有power灯是亮着的,HA灯和status灯都不亮. 用笔记本直连防火墙的mgmt口不亮,c ...
- Inno Setup界面拉伸
1.源起: 源于一个安装包的广告定制.广告客服提供的图片太大,inno setup默认尺寸容不下它,需要扩充,拉宽安装界面尺寸. 以inno setup所附带例子说事,其默认尺寸如下: 2.Scale ...
- iOS 10 之后权限设置
iOS 10 之后权限设置 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Privacy - Camera U ...
- linux命令学习之:read
read命令从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合.该命令可以一次读取多个变量的值,变量和输入的值都需要使用空格隔开.在read命令后面,如果没有指定变量名,读取的数据将被自 ...
- C# mysql 插入数据,中文乱码
用C#操作mysql时, 插入数据中文都是乱码,只显示问号,数据库本身使用的是utf-8字符. 网上百度一下有两种解决办法: 一种是在执行语句前面设置,如:MySQLCommand mCommand ...
- C++中的fstream,ifstream,oftream
https://blog.csdn.net/kingstar158/article/details/6859379 先mark一个大佬的随笔,有时间再回头看 总结: 使用ifstream和ofstre ...
- Django权限系统auth
auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管理系统. 在INSTALLED_APPS中添加'd ...