Hibernate消極式載入Lazy

來源:互聯網
上載者:User

標籤:

Hibernate消極式載入Lazy

消極式載入(lazy load)又稱為懶載入,消極式載入的機制是為了避免一些無謂效能的開銷而提出來的,所謂消極式載入就是當在真正需要資料的時候,才真正執行資料載入操作

如何配置延時載入呢?

在Hibernate中通過對.hbm的lazy屬性來賦值,不同位置出現lazy的作用和取值也是不同的,下面我們來分別介紹

一.Class標籤中的lazy

我們可以看到class標籤中的lazy有兩個取值,分別為true和false,當lazy值為預設值true時,它是消極式載入的,當lazy值為false時,是立即載入的

我們來做例子實驗一下

/* * 部門實體類 */public class Dept {    //不問標號    private Integer deptNo;    private String deptName;    //關聯一個員工集合    private Set<Emp> sets=new HashSet<Emp>();    public Set<Emp> getSets() {        return sets;    }    public void setSets(Set<Emp> sets) {        this.sets = sets;    }    public Integer getDeptNo() {        return deptNo;    }    public void setDeptNo(Integer deptNo) {        this.deptNo = deptNo;    }    public String getDeptName() {        return deptName;    }    public void setDeptName(String deptName) {        this.deptName = deptName;    }    }
<!--Dept.hbm.xml-->
<?
xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.a.entity"> <class name="Dept" table="Dept" lazy="true"> <id name="deptNo"> <generator class="sequence"> <param name="sequence">SEQ_Student</param> </generator> </id> <property name="deptName"></property> <set name="sets" cascade="save-update"> <key column="deptNo"></key> <!-- 多的一方的外建 --> <one-to-many class="Emp" /> </set> </class> </hibernate-mapping>
//測試端代碼@Test    public void toTest()    {            Session session = HibernateUtil.getSession();        Transaction tx = session.beginTransaction();        Dept dept=(Dept)session.load(Dept.class, 1);        System.out.println(dept.getDeptNo());        System.out.println("================================");                tx.commit();        HibernateUtil.CloseSession();    }

這裡注意,session.get()的方法不支援延時載入他會忽略掉類層級的lazy屬性,當lazy為true的時候運行結果如下所示

 

那麼,把lazy改為false時,其他代碼都不變,運行結果如下所示

我們可以看到兩種取值的區別,當lazy屬性值為true時Hibernate並不會一次性載入出所有屬性值,只有當程式需要時才去載入從而減少了和資料庫互動的負擔,提升了程式的效能,這也是消極式載入出現的目的!

二.Set元素中的lazy屬性

如果對象中存在其他實體的集合則需要在hbm檔案中配置set元素來進行表間的映射,而在set元素中也可以添加lazy強延時載入。

set元素中lazy有三個值,false(非延時載入),true(延時載入),extra(加強延時載入)

 

同樣我們用上面的例子做測試

/* * 部門實體類 */public class Dept {    //不問標號    private Integer deptNo;    private String deptName;    //關聯一個員工集合    private Set<Emp> sets=new HashSet<Emp>();    public Set<Emp> getSets() {        return sets;    }    public void setSets(Set<Emp> sets) {        this.sets = sets;    }    public Integer getDeptNo() {        return deptNo;    }    public void setDeptNo(Integer deptNo) {        this.deptNo = deptNo;    }    public String getDeptName() {        return deptName;    }    public void setDeptName(String deptName) {        this.deptName = deptName;    }    }
<?xml version="1.0"?>                                                                                      <!DOCTYPE hibernate-mapping PUBLIC                                                                             "-//Hibernate/Hibernate Mapping DTD 3.0//EN"                                                               "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">                                              <hibernate-mapping package="cn.a.entity">                                                                      <class name="Dept" table="Dept">                                                                           <id name="deptNo">                                                                                             <generator class="sequence">                                                                               <param name="sequence">SEQ_Student</param>                                                                 </generator>                                                                                           </id>                                                                                                      <property name="deptName"></property>                                                                      <set name="sets" cascade="save-update" lazy="true">                                                        <key column="deptNo"></key>        <!-- 多的一方的外建 -->                                                           <one-to-many class="Emp" />                                                                                </set>                                                                                                     </class>                                                                                               </hibernate-mapping>                                                                                                                                                                                                  

這裡我們只比較true和extra的區別

@Test    public void toTest()    {            Session session = HibernateUtil.getSession();        Transaction tx = session.beginTransaction();        Dept dept=(Dept)session.load(Dept.class, 1);        System.out.println(dept.getSets().size());        System.out.println("================================");        for (Emp item : dept.getSets()) {            System.out.println(item.getEmpName());        }        tx.commit();        HibernateUtil.CloseSession();    }

當lazy為true時,運行結果如下

當lazy為extra時,運行結果如下

 

Hibernate消極式載入Lazy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.