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

  • 相关阅读:
    PyQt QFontDialog显示中文
    zetcode :: First programs in PyQt5
    PyQt4 初试牛刀二
    PyQt5实现透明电子时钟
    配置web.xml和glassfish容器实现javaEE表单验证
    nodejs querystring踩坑笔记----只能用于表单提交
    使用JavaEE的ServerAuthModule模块和web.xml进行相应配置,实现对用户的权限控制
    CAS单点登录(SSO)服务端的部署和配置---连接MySQL进行身份认证
    Oracle中碰到的函数和关键字收集
    windows系统局域网内开启远程桌面图解
  • 原文地址:https://www.cnblogs.com/aeon/p/10094577.html
Copyright © 2011-2022 走看看