程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> jpa抓取策略詳解fetch(lazy ,eager)

jpa抓取策略詳解fetch(lazy ,eager)

編輯:關於JAVA

在jpa中jpa默認的加載方式是lazy方式也就是在實際使用到數據的時候才加 載相關數據,使用lazy時可以不用顯示注明fetch=FetchType.LAZY

實體bean:carage

Java代碼

package com.hibernate.jpa.bean1;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Garage {

  /**
  * many to one 多對一
  */
  private Integer gid;
  private String garagenum;
  private Set<Auto> autos = new HashSet<Auto> ();

  @Id @GeneratedValue
  public Integer getGid() {
  return gid;
  }
  public void setGid(Integer gid) {
  this.gid = gid;
  }
  @Column(length=20)
  public String getGaragenum() {
  return garagenum;
  }
  public void setGaragenum(String garagenum) {
  this.garagenum = garagenum;
  }
  @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")
  public Set<Auto> getAutos() {
  return autos;
  }
  public void setAutos(Set<Auto> autos) {
  this.autos = autos;
  }
  public void addGarageAuto(Auto auto) {
  auto.setGarage(this);
  this.autos.add(auto);
  }

}

實體bean:auto

Java代碼

package com.hibernate.jpa.bean1;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Auto {

  /**
  * one to many 一對多關聯
  */
  private Integer autoId;
  private String autotype;
  private String autonum;
  private Garage garage;

  @Id @GeneratedValue
  public Integer getAutoId() {
  return autoId;
  }
  public void setAutoId(Integer autoId) {
  this.autoId = autoId;
  }
  public String getAutotype() {
  return autotype;
  }
  public void setAutotype(String autotype) {
  this.autotype = autotype;
  }
  public String getAutonum() {
  return autonum;
  }
  public void setAutonum(String autonum) {
  this.autonum = autonum;
  }
  @ManyToOne()
  @JoinColumn(name="garageid")
  public Garage getGarage() {
  return garage;
  }
  public void setGarage(Garage garage) {
  this.garage = garage;
  }

}

junit的測試方法

Java代碼

@Test public void query() {
  EntityManagerFactory factory =  Persistence.createEntityManagerFactory("jpa-hibernate");
  EntityManager em = factory.createEntityManager();

  Garage garage = em.find(Garage.class, 1);

  em.close();
  factory.close();
  }

調用query方法的時候發出的sql語句是:

Sql代碼 Hibernate: select garage0_.gid as gid1_0_,  garage0_.garagenum as garagenum1_0_ from Garage garage0_ where  garage0_.gid=?

也就是僅僅獲取了garage對象,而沒有獲取與garage關聯的auto對象

-----------------

(二)在Garage.java中添加fetch=FetchType.EAGER字段

@OneToMany(cascade= {CascadeType.PERSIST},fetch=FetchType.EAGER,mappedBy="garage")
public Set<Auto> getAutos() {
return autos;
}

再次運行query方法,這一次發出的sql語句是:

Sql代碼

Hibernate: select garage0_.gid as gid1_1_, garage0_.garagenum  as garagenum1_1_, autos1_.garageid as garageid3_,  autos1_.autoId as autoId3_, autos1_.autoId as autoId0_0_,  autos1_.autonum as autonum0_0_, autos1_.autotype as autotype0_0_,  autos1_.garageid as garageid0_0_ from Garage garage0_ left  outer join Auto autos1_ on garage0_.gid=autos1_.garageid where  garage0_.gid=?

這一次由於將jpa默認的抓取策略改為fetch=FetchType.EAGER

所以jpa在加載數據的時候一次性的加載了和garage相關聯的數據

說明:由於fetch=FetchType.EAGER加載數據的時候是一次性加載可能會造成 不必要的性能浪費,使用是應該慎重考慮

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved