zoukankan      html  css  js  c++  java
  • 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进行查询(加载)对象时不依赖当前的事务环境(当前环境中可以不存在事务)、也就是查询之前我们不需要开启事务

      

      

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    ubuntu 制做samba
    《Programming WPF》翻译 第4章 前言
    《Programming WPF》翻译 第4章 3.绑定到数据列表
    《Programming WPF》翻译 第4章 4.数据源
    《Programming WPF》翻译 第5章 6.触发器
    《Programming WPF》翻译 第4章 2.数据绑定
    《Programming WPF》翻译 第4章 1.不使用数据绑定
    《Programming WPF》翻译 第5章 7.控件模板
    《Programming WPF》翻译 第5章 8.我们进行到哪里了?
    《Programming WPF》翻译 第5章 5.数据模板和样式
  • 原文地址:https://www.cnblogs.com/aeon/p/10094577.html
Copyright © 2011-2022 走看看