您的位置:首页 > 大数据 > 人工智能

failed to lazily initialize a collection of role 异常

2017-04-10 21:02 375 查看
  最近在通过配置实体类的方式,正向自动扫描注解方式配置的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐