fetch = FetchType.LAZY懒加载

xiaoxiao2021-02-27  415

如果是EAGER,那么表示取出这条数据时,它关联的数据也同时取出放入内存中

如果是LAZY,那么取出这条数据时,它关联的数据并不取出来,在同一个session中,什么时候要用,就什么时候取(再次访问数据库)。

但是,在session外,就不能再取了。用EAGER时,因为在内存里,所以在session外也可以取。

 

一般只在一边设Eager,JPA接口默认为一对多为Lazy,多对一为Eager,但是Hibernate反向工程生成Entity时,多对一为Lazy,需要手动改为Eager

 

而两边都设Eager,那么代码中取一条记录时,会发2次SQL。

 

Tgroup:

Java代码   package com.hibernate.entity;      import java.util.HashSet;      /**   * Tgroup entity. @author MyEclipse Persistence Tools   */   @Entity   @Table(name = "tgroup", catalog = "test")   public class Tgroup implements java.io.Serializable {          // Fields          /**       *        */       private static final long serialVersionUID = -7208715716759269846L;       private Integer id;       private String name;       private Set<Tuser> tusers = new HashSet<Tuser>(0);          // Constructors          /** default constructor */       public Tgroup() {       }          /** full constructor */       public Tgroup(String name, Set<Tuser> tusers) {           this.name = name;           this.tusers = tusers;       }          // Property accessors       @Id       @GeneratedValue       @Column(name = "id", unique = true, nullable = false)       public Integer getId() {           return this.id;       }          public void setId(Integer id) {           this.id = id;       }          @Column(name = "name")       public String getName() {           return this.name;       }          public void setName(String name) {           this.name = name;       }          @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tgroup")       public Set<Tuser> getTusers() {           return this.tusers;       }          public void setTusers(Set<Tuser> tusers) {           this.tusers = tusers;       }      }  

 

 

Tuser:

Java代码   package com.hibernate.entity;      import javax.persistence.Column;      /**   * Tuser entity. @author MyEclipse Persistence Tools   */   @Entity   @Table(name = "tuser", catalog = "test")   public class Tuser implements java.io.Serializable {          // Fields          /**       *        */       private static final long serialVersionUID = -7792597282750540598L;       private Integer id;       private Tgroup tgroup;       private String name;          // Constructors          /** default constructor */       public Tuser() {       }          /** full constructor */       public Tuser(Tgroup tgroup, String name) {           this.tgroup = tgroup;           this.name = name;       }          // Property accessors       @Id       @GeneratedValue       @Column(name = "id", unique = true, nullable = false)       public Integer getId() {           return this.id;       }          public void setId(Integer id) {           this.id = id;       }          @ManyToOne(fetch = FetchType.EAGER)//<span style="color: #ff0000;">注意这行代码</span>                            @JoinColumn(name = "groupid")       public Tgroup getTgroup() {           return this.tgroup;       }          public void setTgroup(Tgroup tgroup) {           this.tgroup = tgroup;       }          @Column(name = "name")       public String getName() {           return this.name;       }          public void setName(String name) {           this.name = name;       }      }    

Java代码   @ManyToOne(fetch = FetchType.EAGER)//注意这行代码   @JoinColumn(name = "groupid")       public Tgroup getTgroup() {           return this.tgroup;       }  

 时

 

Java代码   @Test       public void getUser() {           Session s = sessionFactory.getCurrentSession();           s.beginTransaction();           Tuser user = (Tuser)s.get(Tuser.class1);           System.out.println(user.getName());           s.getTransaction().commit();           System.out.println(user.getTgroup().getName());//这行代码OK,当然,也可以把这行代码放在commit()之前       }  

 

如果

Java代码   @ManyToOne(fetch = FetchType.LAZY)//注意这行代码   @JoinColumn(name = "groupid")       public Tgroup getTgroup() {           return this.tgroup;       }  

时,

Java代码   @Test       public void getUser() {           Session s = sessionFactory.getCurrentSession();           s.beginTransaction();           Tuser user = (Tuser)s.get(Tuser.class1);           System.out.println(user.getName());           s.getTransaction().commit();           System.out.println(user.getTgroup().getName());//这行代码错误,不过,这行代码放在commit()之前,是正确的。       }  
转载请注明原文地址: https://www.6miu.com/read-1382.html

最新回复(0)