引起原因: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的更多相关文章

  1. ssh框架错误:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role。

    在做ssh项目练习的时候出现问题: org.hibernate.LazyInitializationException: failed to lazily initialize a collectio ...

  2. 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 ...

  3. Hibernate加载数据失败failed to lazily initialize a collection of role

    在测试获取数据库中的数据或者在页面获取时,有时会遇到这样的错误提示: failed to lazily initialize a collection of role: com.exam.entity ...

  4. 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 ...

  5. 【转】hibernate懒加载的问题,failed to lazily initialize a collection of role

    hibernate懒加载的问题,failed to lazily initialize a collection of role hibernate懒加载的问题,failed to lazily in ...

  6. org.hibernate.LazyInitializationException: failed to lazily initialize

    今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的s ...

  7. 问题:org.hibernate.LazyInitializationException: failed to lazily initialize

    今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的s ...

  8. UI Automator Viewer获取手机镜像时报错

    使用UI Automator Viewer获取手机镜像时报错,具体信息如下: Error while obtaining UI hierarchy XML file: com.android.ddml ...

  9. 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 ...

随机推荐

  1. 微信小程序 错误记录

    1.报错this.getUserInfo(this.setData) is not a function;at pages/index/index onShow function;at api req ...

  2. [z]c++ 和 java 利用protobuf 通讯

    [z]http://andinker.iteye.com/blog/1979428  java端的具体步骤如下: 1.首先下载 下载protobuf 编译工具   http://code.google ...

  3. svn-经常遇到问题解答办法积累(一)

    1.对于一个SVN使用新手,第一步,肯定是如何获取代码到本地指定的目录. 步骤: (1)新建一个存放svn中某一个代码库的目录,加入该目录命名为:Proj1SVN (2)右键鼠标,选择SVN Chec ...

  4. Django 导入css文件,样式不起作用。Resource interpreted as Stylesheet but transferred with MIME type application/x-css

    笔者今天在模板中加载css文件时,发现 css样式能够下载再来却无法起作用,而且,图片.js都能够正常使用. 并且 浏览器提示: Resource interpreted as Stylesheet ...

  5. ie8,9不支持indexOf解决办法,纯拷贝

    原文在这里,大家快去点啊 自从开始工作后,就没有再碰过原型链了,今天遇到ie8不认识indexOf的时候才发现原型这么嚣张,,哈哈 把代码粘过来,以后留着看 //添加数组IndexOf方法 if (! ...

  6. socket 进阶

    1.验证客户端链接的合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现 import socket import os impor ...

  7. SqlServer添加触发器死锁的原因

    之前遇到过SqlServer添加触发器死锁的情况,纠结了很长时间 最近发现原来是因为我在建表的时候,把id设成主键后,系统默认了加一个聚集的索引 就是聚集索引把表锁住了

  8. (转)关于X64位系统IIS7下支持32位asp.net程序

    最近在windows2008 x64位系统下的IIS7下部署asp.net程序. vs2005或vs2008默认的情况下是Any cpu 的也就是支持x86和x64两种系统的.可我的程序在引用了一个三 ...

  9. JianShu_failban2实现动态屏蔽的功能

    一,首先是服务安装 #vim /etc/yum.repos.d/Centos-Base.repo 在最新新增 [atrpms] name=Red Hat Enterprise Linux $relea ...

  10. python——处理xls表格

    因为工作需要,现有一个运营商导出的xls固定电话话费账单. 账单比较详细,而我最终需要的数据只有那个号码这个月用了多少话费的统计结果. 当年没有好好学office,以致于无从下手.泪奔/(ㄒoㄒ)/~ ...