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 层 ...
随机推荐
- 简单案列完美搞定Mvc设计模式
一个小列子搞定Mvc模式,包括数据库以及如何提高用户体验度 1.首先来web.xml配置servlet的访问路径: <?xml version="1.0" encoding= ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- 字符串转换JSON 的方法
function (sJSON) { if (window.JSON) { return window.JSON.parse(sJSON); } else { return eval('(' + sJ ...
- Controller接收处理json、xml格式数据
1.RequestBody接收json格式的数据,并直接转为对象. User.java使用lombok依赖包 @Data @AllArgsConstructor @NoArgsConstructor ...
- js倒计时小插件(兼容大部分浏览器)
精确到天的倒计时 <script language="JavaScript"> <!-- // (c) Henryk Gajewski var urodz= ne ...
- Win10激活方法(企业版)
Win10激活 注意:以管理员身份运行,需要电脑有网(亲测激活企业版没问题) 然后一条一条复制执行 slmgr /ipk NPPR9-FWDCX-D2C8J-H872K-2YT43 slmgr /sk ...
- 使用notepad++远程编辑Linux文档
上一篇中,我写了如何使用使用ftp服务器实现很方便的通信,这一篇我分享一个使用notepad++的一个NPPFTP插件远程编辑Linux中的文档的小技巧. 首先要确保你的Linux的ftp服务已经打开 ...
- 01_1_Socket实现
01_1_Socket实现 1.什么是MIME Multipurpos Internet Mail Extension 指明白传送内容的格式 最早用于邮件附件 2.HTTP协议基础 HTTP(Hype ...
- iOS--获取文件目录的方法
很多文章都有写这个问题,我只是为了记录一下,免得总翻书... 1.Documents 目录: 你应该将所有的应用程序数据文件写入到这个目录下.这个目录用于存储用户数据或其它应该定期备份的信息. 2.L ...
- atomic nonatomic区别
摘要 atomic和nonatomic区别用来决定编译器生成的getter和setter是否为原子操作.atomic提供多线程安全,是描述该变量是否支持多线程的同步访问,如果选择了atomic 那么就 ...