failed to lazily initialize a collection of role 异常
最近在通过配置实体类的方式,正向自动扫描注解方式配置的hibernate类文件来生成数据库的方法搭建环境,遇到了许多问题。
通过数据库配置hibernate的时候,大家都知道是在实体类对应生成的.hbm.xml文件中查看一对多和多对多的关系。
当报failed to lazily initialize a collection of role异常的时候,往往是因为懒加载的问题导致的。
可以在.hbm.xml文件中,将lazy="false",这样就不会报这个异常了。
但是在自动扫描注解方式配置的hibernate类文件时,如何将懒加载改为false呢?
只需要一句话,在注解上添加fetch=FetchType.EAGER便可
@OneToMany(mappedBy="user",fetch=FetchType.EAGER)
举个栗子:
package com.maya.entity; import java.util.ArrayList;
import java.util.List; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator;
import org.springframework.context.annotation.Lazy; @Entity
@Table(name="t_user") public class User { private Integer id;
private String password;
private String ename;
private String sex;
//private String dept;
//private Dept dept;
private String tel;
private String description; private List<WarehouseMain> warehouseMainList=new ArrayList<WarehouseMain>();
private List<ReWarehouseMain> reWarehouseMainList=new ArrayList<ReWarehouseMain>();
private List<SellMain> sellMainList=new ArrayList<SellMain>();
private List<ReSellMain> reSellMainList=new ArrayList<ReSellMain>(); @Id
@GenericGenerator(name = "generator", strategy = "native")
@GeneratedValue(generator = "generator")
@Column(name = "id", length=11)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "password", length = 20)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "ename", length = 20)
public String getEname() {
return ename;
} public void setEname(String ename) {
this.ename = ename;
}
@Column(name = "sex", length = 10)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
/*
@ManyToOne
@JoinColumn(name="dept_id")
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}*/
@Column(name = "tel", length = 20)
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Column(name = "description", length = 100)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@OneToMany(mappedBy="user",fetch=FetchType.EAGER)
public List<WarehouseMain> getWarehouseMainList() {
return warehouseMainList;
}
public void setWarehouseMainList(List<WarehouseMain> warehouseMainList) {
this.warehouseMainList = warehouseMainList;
}
@OneToMany(mappedBy="user",fetch=FetchType.EAGER)
public List<ReWarehouseMain> getReWarehouseMainList() {
return reWarehouseMainList;
}
public void setReWarehouseMainList(List<ReWarehouseMain> reWarehouseMainList) {
this.reWarehouseMainList = reWarehouseMainList;
}
@OneToMany(mappedBy="user",fetch=FetchType.EAGER)
public List<SellMain> getSellMainList() {
return sellMainList;
}
public void setSellMainList(List<SellMain> sellMainList) {
this.sellMainList = sellMainList;
}
@OneToMany(mappedBy="user",fetch=FetchType.EAGER)
public List<ReSellMain> getReSellMainList() {
return reSellMainList;
}
public void setReSellMainList(List<ReSellMain> reSellMainList) {
this.reSellMainList = reSellMainList;
} }
这个实体类里,对应有四个一对多的外键关系,每一个一对多的关系查询的时候都涉及到一个懒加载,所以说,每一个OneToMany上都要添加fetch=FetchType.EAGER
failed to lazily initialize a collection of role 异常的更多相关文章
- failed to lazily initialize a collection of role
可能修复了一个重大的偶尔发生的几乎难以察觉的并且到现在我也没能理解的bug...有时(经常)调用updateNotNullfield方法(原理是从数据库中get一个对象,然后把原对象中非空的值赋予它, ...
- Hibernate加载数据失败failed to lazily initialize a collection of role
在测试获取数据库中的数据或者在页面获取时,有时会遇到这样的错误提示: failed to lazily initialize a collection of role: com.exam.entity ...
- 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 ...
- ssh框架错误:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role。
在做ssh项目练习的时候出现问题: org.hibernate.LazyInitializationException: failed to lazily initialize a collectio ...
- 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 ...
- 多对多关联懒加载导致failed to lazily initialize a collection of role: 实体类, could not initialize proxy - no Session 追加配置fetch = FetchType.EAGER解决
一篇文章需要关联很多个标签,所以他们呈一对多(多对多)的关系 org.springframework.web.util.NestedServletException: Request processi ...
- ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: com.goodfan.entity.BeanA.beanB, no session or session was closed
1. 问题, 当使用JSONArray.fromObject(List<BeanA>)时, beanA中含有BeanB的属性beanB时,会报这个错 2. 解决办法: 使用jsonconf ...
- failed to lazily initialize a collection of role:XXX, no sessi
系统 框架 springMVC+hibernate 这种情况 由于 hibernate 的 懒汉机制,和 Spring 事务机制(不确定)造成的 由于 spring 配置的时候,在service 层 ...
随机推荐
- Autoit3 如何捕足控件
以任务管理器为例,在命令行提示符下输入taskmgr.exe 接下来识别这个窗口上的控件 在AU3 中提供了一个捕捉控件的工具是Au3Info.exe 这里记录了控件的标题,控件的类型,控件的坐标和 ...
- [论文理解] MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications Intro MobileNet 我 ...
- MyBatis归纳
SqlSessionTemplate详解 SqlSessionTemplate类是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL ...
- JAVA并发编程:相关概念及VOLATILE关键字解析
一.内存模型的相关概念 由于计算机在执行程序时都是在CPU中运行,临时数据存在主存即物理内存,数据的读取和写入都要和内存交互,CPU的运行速度远远快于内存,会大大降低程序执行的速度,于是就有了高速缓存 ...
- C09 指针
目录 指针相关概念 指针变量 null指针 指针的算术运算 指针数组 指向指针的指针 传递指针给函数 从函数返回指针 指针相关概念 变量 如果在程序中定义了一个变量,在对程序进行编译时,系统就会为这个 ...
- java基础—线程(二)
一.线程的优先级别
- ps基础入门快捷方法总结
1. 快速打开文件 双击Photoshop的背景空白处(默认为灰色显示区域)即可打开选择文件的浏览窗口. 2. 随意更换画布颜色 选择油漆桶工具并按住Shift点击画布边缘,即可设置画布底色为当前选择 ...
- java利用SuffixFileFilter统计目录下特定后缀名文件的数目
/** * 文件处理类 * @author zhangcd * @date 2017年1月3日 */ public class FileUtil { /** * 得到所有后缀的数目 * * @para ...
- Java Object类 instanceof关键字 练习:判断是否为同一人 集合按照人的年龄排序,如果年龄相同按名字的字母顺序升序 Comparator比较器
package com.swift; public class Same_Person_Test { public static void main(String[] args) { /* * Obj ...
- c++调用系统关机命令 c++调用暂停命令
#include<stdlib.h> int main() { //调用系统dos命令 system("shutdown -s -t 120"); ; } system ...