• 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 || 点返回首页

  • 相关阅读:
    Windbg使用
    C#与C++之间类型的对应
    Hook CreateProcess
    基于EasyHook实现监控explorer资源管理器文件复制、删除、剪切等操作
    GUID和UUID、CLSID、IID 区别及联系
    hook C++
    EasyHook Creating a remote file monitor
    hook工具
    EasyHook
    Hook exe 和 file
  • 原文地址:https://www.cnblogs.com/aeon/p/10094577.html
Copyright © 2020-2023  润新知