1. 程式人生 > >hibernate延遲載入以及no-session

hibernate延遲載入以及no-session

延遲載入:延遲載入(lazy load懶載入)是當在真正需要資料時,才執行SQL語句進行查詢,避免了無謂的效能開銷。

 延遲載入分類:

 01.類級別的查詢策略

 02.一對多和多對多關聯的查詢策略

 03.多對一關聯的查詢策略

類級別的查詢策略

     1.類級別可選的檢索策略包括立即檢索延遲檢索預設為延遲檢索

1.1立即檢索:立即載入檢索方法指定的物件,立即傳送SQL
1.2 延遲檢索:延遲載入檢索方法制定的物件.在使用具體的屬性時,再進行載入,才傳送SQL

   2.無論<class>元素的lazy屬性是true還是falseSessionget()方法及Query

list()方法在類級別總是使用立即檢索策略

 3.<class>元素的lazy屬性為true或預設值,Sessionload()方法不會執行查詢資料庫表的SELECT語句,僅返回代理類物件的例項,該代理類例項有如下特徵:

3.1 Hibernate在執行時採用CGLIB工具動態生成3.2 Hibernate建立代理類例項時,僅初始化其OID屬性3.3在應用程式第一次訪問代理類例項的非OID屬性時,Hibernate會初始化代理類例項

    3.4注意:類級別檢索策略僅適用於load()方法


(立即載入,延遲載入等)檢索,通過set元素的lazy屬性設定。

01.一對多或者多對多檢索策略由

lazyfetch共同確定

    Lazy:決定關聯物件初始化時機

    Fetch:決定SQL語句構建形式

02.fetch取值

    Join:迫切左外連線

    Select:多條簡單SQL(預設值)

    Subselect:子查詢

03.fetchlazy組合

  解析:fetch=join lazy會被忽略,迫切左外連線的立即檢索

        Fetch=”select” lazy=”false”  多條簡單SQL立即檢索

        Fetch=”select” lazy=”true”  多條語句延遲檢索

        Fetch=”select” lazy=”extra”  多條語句及其懶惰檢索

        Fetch=”subselect” lazy=”false”  子查詢立即檢索

        Fetch=”subselect” lazy=”true”  子查詢延遲檢索

        Fetch=”subselect” lazy=”extra”  子查詢及其懶惰檢索

Extra:極其懶惰,只有訪問集合物件的屬性時才會載入,訪問集合本身的屬性時(例如,集合大小,生成count),不會立即載入。

注意:querylist()會忽略對映檔案配置的左外連線查詢,此時lazy屬性重新生效。



具體案例

首先我們用的hibernate.hbm.xml都是一個的 直接上傳一個就ok. 連線資料庫 對應哪個包下仔細看一下。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

   <!-- <session-factory>

        <property name="myeclipse.connection.profile">JDBC for MySQL</property>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://localhost:3306/book</property>

        <property name="connection.username">root</property>

        <property name="connection.password"></property>

        <property name="hibernate.show_sql">true</property>

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.format_sql">true</property>
        <!–生成特定資料庫sql–>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!–thread指定當前執行緒來跟蹤管理–>
        <property name="current_session_context_class">thread</property>


        <mapping resource="Dog.xml"></mapping>

    </session-factory>
-->

    <session-factory>
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">sll</property>
        <property name="connection.password">sll</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>
        <property name="connection.autocommit">true</property>
     <!--   <mapping resource="cn/happy/hibernate01/Cat.hbm.xml"/>
        <mapping resource="cn/happy/hibernate02/Dept.hbm.xml"/>-->
     <!--   <mapping resource="cn/happy/hibernate03hql/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate04pagelist/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate06mapping/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate06mapping/Dept.hbm.xml"/>

        <mapping resource="cn/happy/hibernate07onttomany/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate07onttomany/Dept.hbm.xml"/>
-->
       <!-- <mapping resource="cn/happy/hibernate08save/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate08save/Dept.hbm.xml"/>

      <mapping resource="cn/happy/hibernate09manytomany/Employee.hbm.xml"/>
        <mapping resource="cn/happy/hibernate09manytomany/Project.hbm.xml"/>
        <mapping resource="cn/happy/hibernate11lazyLoad/Student.hbm.xml"/>-->
   <!--   <mapping resource="cn/happy/hibernate11lazyLoad/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate11lazyLoad/Dept.hbm.xml"/>-->
        <!--no-session-->
      <!--  <mapping resource="cn/happy/hibernate12nosession/Emp.hbm.xml"/>
        <mapping resource="cn/happy/hibernate12nosession/Dept.hbm.xml"/>
-->
        <mapping resource="cn/happy/hibernate10opensessioninview/Student.hbm.xml"/>
        <mapping class="cn.happy.hibernate13zj.Dept"></mapping>
        <mapping class="cn.happy.hibernate14onetoone.Humen"></mapping>
        <mapping class="cn.happy.hibernate14onetoone.Card"></mapping>
    </session-factory>
</hibernate-configuration>


然後因為這個沒有用到註解 所以我們需要小配置

首先我們看一下實體類。

package cn.happy.hibernate11lazyLoad;

import java.util.Date;

/**
 * Created by linlin on 2017/10/9.
 */
public class Student {
    private Integer stuno;
    private String stuname;
    private Integer stuage;
    private Date studate;

    public Integer getStuno() {
        return stuno;
    }

    public void setStuno(Integer stuno) {
        this.stuno = stuno;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Integer getStuage() {
        return stuage;
    }

    public void setStuage(Integer stuage) {
        this.stuage = stuage;
    }

    public Date getStudate() {
        return studate;
    }

    public void setStudate(Date studate) {
        this.studate = studate;
    }
}


每個 案例也就那幾步 

小配置 :

<?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.happy.hibernate11lazyLoad">
    <class name="Student" table="StudentNew" schema="sll"  lazy="true">
        <id name="stuno" column="stuno">
           <generator class="native"></generator>
        </id>
        <property name="stuname" />
       <property name="stuage"/>
        <property name="studate"/>
    </class>
</hibernate-mapping>


測試類:

package cn.happy.test;


import cn.happy.hibernate11lazyLoad.Dept;
import cn.happy.hibernate11lazyLoad.Emp;
import cn.happy.hibernate11lazyLoad.Student;
import cn.happy.hibernate12nosession.biz.HibernateBiz;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Set;

/**
 * Created by linlin on 2017/9/22.
 */
public class test1009 {
    Configuration cfg;
    Transaction tx;
    Session session;
    SessionFactory factory;

    @Before
    public void myBefore() {
        //建立配置物件
        cfg = new Configuration().configure("Hibernate.cfg.xml");
        //根據配置物件建立SessionFactory
        factory = cfg.buildSessionFactory();
        //根據SessionFactory建立Session
        session = factory.openSession();
        //在Session建立後開啟事務
        tx = session.beginTransaction();
    }


    @Test
    public void testmanyToOne() {
        Dept dept = session.get(Dept.class, 14);
        Set<Emp> emps = dept.getEmps();
        emps.size();
    }

    @Test
    public void testmanyToOne0() {
        Student student = session.load(Student.class, 1);
    }



}



這裡下面的方法是上面測試 

下面這個是實體類和小配置是上面的方法

package cn.happy.hibernate11lazyLoad;

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

/**
 * Created by linlin on 2017/9/24.
 */
public class Dept {
    private Integer deptno;
    private String deptname;

   private Set<Emp> emps=new HashSet<Emp>();

    public Set<Emp> getEmps() {
        return emps;
    }

    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }

    public Dept() {
    }

    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;
    }
}


package cn.happy.hibernate11lazyLoad;

/**
 * Created by linlin on 2017/9/24.
 */
public class Emp {
    private Integer empno;
    private String ename;

   private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }
}

小配置:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.hibernate08save">
    <class name="cn.happy.hibernate11lazyLoad.Dept" table="Dept" schema="sll">
        <id name="deptno" column="deptno">
            <generator class="native"/>
        </id>
        <property name="deptname" column="deptname"/>
        <set name="emps" lazy="false" fetch="join">
            <key column="deptno"/>
            <one-to-many class="Emp"/>
        </set>
    </class>


</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.hibernate08save">
    <class name="cn.happy.hibernate11lazyLoad.Emp" table="Emp" schema="sll">
        <id name="empno" column="empno">
            <generator class="native"/>
        </id>
        <property name="ename" column="enmae"/>
        <many-to-one name="dept" column="deptno" class="Dept"></many-to-one>
    </class>


</hibernate-mapping>


上面是延遲載入的 案例

下面是no-session


biz:

package cn.happy.hibernate12nosession.biz;

import cn.happy.hibernate12nosession.Emp;
import cn.happy.hibernate12nosession.dao.HibernateDAO;
import cn.happy.hibernate12nosession.util.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Transaction;

import java.io.Serializable;

/**
 * Created by linlin on 2017/10/9.
 */
public class HibernateBiz {

    HibernateDAO dao=new HibernateDAO();
    public Object get(Class clazz, Serializable id) {
        Transaction tx = HibernateUtil.currentSession().beginTransaction();
        Object obj= dao.get(clazz, id);
       /* Emp emp=(Emp)obj;
        emp.getEname();*/
       if(!Hibernate.isInitialized(obj)){
           Hibernate.initialize(obj);
       }
        System.out.println("==============================================");
        tx.commit();
        HibernateUtil.closeSession();
        return obj;
    }
}

dao:
package cn.happy.hibernate12nosession.dao;



import cn.happy.hibernate10opensessioninview.util.HibernateUtil;

import java.io.Serializable;

/**
 * Created by linlin on 2017/10/9.
 */
public class HibernateDAO {

    public Object get(Class clazz, Serializable id) {
Object result= HibernateUtil.getSession().load(clazz,id);
return result;

    }
}


util:

package cn.happy.hibernate12nosession.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Created by linlin on 2017/10/6.
 */
public class HibernateUtil {
    //getCurrentSession()底層實現原理
    //ThreadLocal變數
    public static final ThreadLocal<Session> threadTL=new ThreadLocal<Session>();

    //我想直接呼叫一個方法,獲取Session
    //定義一個sessionFactory物件
    private static SessionFactory factory;
    private static Configuration cfg;
    static{
        cfg=new Configuration().configure();
        factory=cfg.buildSessionFactory();
    }
    //提供一個靜態方法
    public static Session currentSession(){
        Session session=threadTL.get();
        if(session==null){  //當前執行緒中沒有session物件
            session=factory.openSession();
            threadTL.set(session);
        }
        return session;

    }
    //關閉session
    public static void closeSession(){
        //獲取執行緒中的session
        Session session = threadTL.get();
        if (session!=null) {
            threadTL.set(null);
            session.close();
        }
    }

}

Dept  Emp和上面的一樣的

測試類

package cn.happy.test;



import cn.happy.hibernate12nosession.Emp;
import cn.happy.hibernate12nosession.biz.HibernateBiz;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;

import java.util.Set;

/**
 * Created by linlin on 2017/9/22.
 */
public class test1009_01 {
    Configuration cfg;
    Transaction tx;
    Session session;
    SessionFactory factory;

    @Before
    public void myBefore() {
        //建立配置物件
        cfg = new Configuration().configure("Hibernate.cfg.xml");
        //根據配置物件建立SessionFactory
        factory = cfg.buildSessionFactory();
        //根據SessionFactory建立Session
        session = factory.openSession();
        //在Session建立後開啟事務
        tx = session.beginTransaction();
    }



    //no-session
    @Test
    public void loadGetTest() {
        HibernateBiz biz = new HibernateBiz();
       Emp emp = (Emp) biz.get(Emp.class, 1);
        System.out.println(emp.getEname());
    }
}