1. 程式人生 > >dljd_026_兩種方式獲取到的session查詢時對事務環境的不同依賴

dljd_026_兩種方式獲取到的session查詢時對事務環境的不同依賴

一、測試兩種獲取到的session在查詢中的表現

  1.1通過getCurrentSession獲取到的session查詢物件

  1.1.1get方法通過getCurrentSession獲取到的session查詢物件  

package edu.aeon.test;

import org.hibernate.Session;

import edu.aeon.aeonutils.GetSessionUtil;
import edu.aeon.beans.Student;

/**
 * [說明]:測試兩種獲取到的session在查詢中(對事務需求)的表現
 *         1、用getCurrentSession()獲取到的事務 :必須依賴當前的事務環境、所以當前環境中必須要有事務
 *         2、用openSession()獲取到的事務   :不依賴當前事務(也就是在當前環境中沒有事務也可進行查詢操作)
 * 
@author aeon * */ public class TestSelect { public static void select() { //此處的session是通過getCurrentSession獲取到的 Session session=GetSessionUtil.getSession(); Student student = session.get(Student.class, 1); //Student student = session.load(Student.class, 1);
System.out.println(student); } public static void main(String[] args) { select(); } }

測試結果截圖:

  

  1.1.2load方法通過getCurrentSession獲取到的事務查詢物件

  

package edu.aeon.test;

import org.hibernate.Session;

import edu.aeon.aeonutils.GetSessionUtil;
import
edu.aeon.beans.Student; /** * [說明]:測試兩種獲取到的session在查詢中(對事務需求)的表現 * 1、用getCurrentSession()獲取到的事務 :必須依賴當前的事務環境、所以當前環境中必須要有事務 * 2、用openSession()獲取到的事務 :不依賴當前事務(也就是在當前環境中沒有事務也可進行查詢操作) * @author aeon * */ public class TestSelect { public static void select() { //此處的session是通過getCurrentSession獲取到的 Session session=GetSessionUtil.getSession(); //Student student = session.get(Student.class, 1); Student student = session.load(Student.class, 1); System.out.println(student); } public static void main(String[] args) { select(); } }

  測試結果:

  

  1.2通過openSession獲取到的session查詢物件

   1.2.1get方法通過openSession獲取到的session查詢物件

  

package edu.aeon.test;

import org.hibernate.Session;

import edu.aeon.aeonutils.GetSessionUtil;
import edu.aeon.beans.Student;

/**
 * [說明]:測試兩種獲取到的session在查詢中(對事務需求)的表現
 *         1、用getCurrentSession()獲取到的事務 :必須依賴當前的事務環境、所以當前環境中必須要有事務
 *         2、用openSession()獲取到的事務   :不依賴當前事務(也就是在當前環境中沒有事務也可進行查詢操作)
 * @author aeon
 *
 */
public class TestSelect {
    public static void select() {
        //此處的session是通過getCurrentSession獲取到的
        Session session=GetSessionUtil.getSessionFactory().openSession();
        Student student = session.get(Student.class, 1);
        //Student student = session.load(Student.class, 1);
        System.out.println(student);
    }
    public static void main(String[] args) {
        select();
    }
}

測試結果截圖:

  

  1.2.2load方法通過openSession獲取到的session查詢物件

  

package edu.aeon.test;

import org.hibernate.Session;

import edu.aeon.aeonutils.GetSessionUtil;
import edu.aeon.beans.Student;

/**
 * [說明]:測試兩種獲取到的session在查詢中(對事務需求)的表現
 *         1、用getCurrentSession()獲取到的事務 :必須依賴當前的事務環境、所以當前環境中必須要有事務
 *         2、用openSession()獲取到的事務   :不依賴當前事務(也就是在當前環境中沒有事務也可進行查詢操作)
 * @author aeon
 *
 */
public class TestSelect {
    public static void select() {
        //此處的session是通過getCurrentSession獲取到的
        Session session=GetSessionUtil.getSessionFactory().openSession();
        //Student student = session.get(Student.class, 1);
        Student student = session.load(Student.class, 1);
        System.out.println(student);
    }
    public static void main(String[] args) {
        select();
    }
}

結果截圖:

  

  結論:

    1.不管是用get還是load通過getCurrentSession獲取到的session進行查詢(載入)物件時必須依賴當前的事務環境(當前環境中必須存在事務)、也就是在查詢之前必須要有事務、必須手動開啟(session.beginTransaction())。

    2.不管是用get還是load通過openSession獲取到的session進行查詢(載入)物件時不依賴當前的事務環境(當前環境中可以不存在事務)、也就是查詢之前我們不需要開啟事務