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 层 ...
随机推荐
- codeforce Gym 100418K Cards (概率,数学)
题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...
- 使用Python-OpenCV向图片添加噪声(高斯噪声、椒盐噪声)
在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声.Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现. 代码: import numpy as np impo ...
- Problem O: 国家排序
Problem O: 国家排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 405 Solved: 253[Submit][Status][Web ...
- Spring boot 集成Kafka
搭建Kafka集群,参考: https://www.cnblogs.com/jonban/p/kafka.html 源码示例如下: 1.新建 Maven 项目 kafka 2.pom.xml < ...
- iframe的document操作
导语: 在我写网页代填插件的时候,有遇到拿不到input元素的时候,这时候我去看元素布局,发现有些网站登录那一块是用iframe标签写的,这时候我需要取到的那就是iframe标签下input元素 1. ...
- jQuery向界面输出时保留两位小数
通过JSTL下的<fmt:formatNumber>标签实现,具体实现代码如下: <%@ taglib uri="http://java.sun.com/jsp/jstl/ ...
- Voyager的安装及配置文件
使用代理服务器安装laravel http_proxy=http://localhost:1080 composer create-project --prefer-dist laravel/lara ...
- mysql 的 case when 用法
正确的格式: case when condition then result when condition then result when condition then result else re ...
- VUE2.0声明周期钩子:不同阶段不同钩子的开启
- python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)
什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: 一 from threading import Thread ...