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 warehouseMainList=new ArrayList();
    private List reWarehouseMainList=new ArrayList();
    private List sellMainList=new ArrayList();
    private List reSellMainList=new ArrayList();
    
    @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 getWarehouseMainList() {
        return warehouseMainList;
    }
    public void setWarehouseMainList(List warehouseMainList) {
        this.warehouseMainList = warehouseMainList;
    }
    @OneToMany(mappedBy="user",fetch=FetchType.EAGER)
    public List getReWarehouseMainList() {
        return reWarehouseMainList;
    }
    public void setReWarehouseMainList(List reWarehouseMainList) {
        this.reWarehouseMainList = reWarehouseMainList;
    }
    @OneToMany(mappedBy="user",fetch=FetchType.EAGER)
    public List getSellMainList() {
        return sellMainList;
    }
    public void setSellMainList(List sellMainList) {
        this.sellMainList = sellMainList;
    }
    @OneToMany(mappedBy="user",fetch=FetchType.EAGER)
    public List getReSellMainList() {
        return reSellMainList;
    }
    public void setReSellMainList(List reSellMainList) {
        this.reSellMainList = reSellMainList;
    }
    
    
}

你可能感兴趣的:(WEB后端)