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

  • 相关阅读:
    常用字段类型
    触发事件中的sender
    $符号基本用法$() ${} $(())
    expect语法
    apt 和 apt-get的区别
    python字符串中 r'', b'', u'', f'' 的含义
    linux正则表达式
    Linux下apt-get命令详解(安装、卸载、更新、查询软件包)
    创建linux service服务
    Ubuntu下deb包的解包、打包
  • 原文地址:https://www.cnblogs.com/aeon/p/10094577.html
Copyright © 2011-2022 走看看