最近在通過配置實體類的方式,正向自動掃描注解方式配置的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