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

  • 相关阅读:
    Office相关
    Eclipse常用设置
    Google logos 纪念电吉他大师莱斯·保罗(LesPaul)演示
    强烈推荐SQL Prompt 3.8,并发布SQL Prompt 3.8 ,SQL Refator 的xxx
    C#命令行编辑器csc.exe
    JSP中文乱码问题 页面经过过滤器后得到的是中文,但插入到MYSQL数据库却成了“?”为什么?
    (转贴)来谈谈SQL数据库中"简单的"SELECT TOP—可能有你从未注意到的细节
    C#Winform限制Textbox只能输入数字
    VPC2007虚拟机与主机的互连互通方法
    邮件会消亡是无稽之谈
  • 原文地址:https://www.cnblogs.com/aeon/p/10094577.html
Copyright © 2011-2022 走看看