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 层 ...
随机推荐
- javaSe-SimpleDateFormat
SimpleDateFormat呢是一种可以将字符串转为日期或者日期转换成字符串的功能强大的不得了的类: import java.text.ParseException;import java.tex ...
- python基础教程总结8——特殊方法,属性,迭代器,生成器,八皇后问题
1. 重写一般方法和特殊的构造方法 1.1 如果一个方法在B类的一个实例中被调用(或一个属性被访问),但在B类中没有找到该方法,那么会去它的超类A里面找. class A: ... def hello ...
- 小常识:变量的修饰符和DEMO
public static string ss = "这是全局静态变量";//生命周期:程序结束为止,可以修改 public string s = "这是全局变量&quo ...
- [web开发] Vue + spring boot + echart 微博爬虫展示平台
1.微博登录 2.爬取数据 3.mysql存储 4.pyechart本地展示 5.用vue搭建网站web展示 先放图: 1.微博登录 新浪微博的登录不是简单的post就能解决的,他的登录有加密,所以我 ...
- ajax的dataType有哪些类型?
ajax的dataType有哪些类型? 格式为:dataType:"xxx", •"xml": 返回 XML 文档,可用 jQuery 处理 •"ht ...
- Dojo的define接口
http://blog.csdn.net/lovecarpenter/article/details/53979717 第三种用法用的最多. 此接口用于定义模块: define([],function ...
- 使用vs2013打开VS2015的工程文件的解决方案(适用于大多数vs低版本打开高版本)
前言:重装系统前我使用的是vs2015(有点装*),由于使用2015实在在班上太另类了, 导致我想在其他同学的vs下看一看我写的代码都无法达成! 而且最关键的是交作业的时候,老师的2013也没有办法打 ...
- iOS微信小视频优化心得
小视频是微信6.0版本重大功能之一,在开发过程中遇到不少问题.本文先叙述小视频的产品需求,介绍了几个实现方案,分析每个方案的优缺点,最后总结出最优的解决方案. 小视频播放需求 可以同时播放多个视频 用 ...
- runtime实践之Method Swizzling
利用 Objective-C 的 Runtime 特性,我们可以给语言做扩展,帮助解决项目开发中的一些设计和技术问题.这一篇,我们来探索一些利用 Objective-C Runtime 的黑色技巧.这 ...
- stringByAppendingString和stringByAppendingPathComponent
NSString提供了两个拼串的方法: /** * @brief 简单的字符串拼接,头文件 NSString (NSStringExtensionMethods) * * @param aString ...