• Hibernate openSession() 和 getCurrentSession的区别 .


    Hibernate openSession() 和 getCurrentSession的区别

    getHiberanteTemplate 、getCurrentSession和OpenSession
    采用getCurrentSession()创建的Session会绑定到当前的线程中去、而采用OpenSession()则不会。

    采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用OpenSession()必须手动关闭。

    采用getCurrentSession()需要在Hibernate.cfg.xml配置文件中加入如下配置:

    如果是本地事物,及JDBC一个数据库:

    <propety name=”Hibernate.current_session_context_class”>thread</propety>

    如果是全局事物,及jta事物、多个数据库资源或事物资源:

    <propety name=”Hibernate.current_session_context_class”>jta</propety>

    使用spring的getHiberanteTemplate 就不需要考虑事务管理和session关闭的问题:

    public List getEntityCriteria(final DetachedCriteria detachedCriteria) {
            return (List) getHibernateTemplate().executeFind(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session)
                                throws HibernateException {
                            Criteria criteria = detachedCriteria
                                    .getExecutableCriteria(session);
                            return criteria.list();
                        }
                    });
        }
        public List getEntityCriteriaByPage(final DetachedCriteria detachedCriteria,final Page page) {
            return (List) getHibernateTemplate().executeFind(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session)
                                throws HibernateException {
                            Criteria criteria = detachedCriteria
                                    .getExecutableCriteria(session);

                            criteria.setFirstResult(page.getFirstItemPos());
                            criteria.setMaxResults(page.getPageSize());
                            return criteria.list();
                        }
                    });
        }

    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Department.class);

    detachedCriteria.add(Restrictions.eq("eid", seacher.getEid()));//企业id为条件查询

    detachedCriteria.add(Restrictions.in("dprtid", ids));//根据id数组查询部门

    detachedCriteria.add(Restrictions.like("name", "%" + seacher.getName()+ "%"));//名称模糊查询

    getEntityCriteriaByPage(detachedCriteria,pageinfo);

    经过检查激活连接为0,被使用的连接总是为1(应为自己测试)

    package com.myj.hibernate.model; 
    
      import org.hibernate.HibernateException; 
    
      import org.hibernate.SessionFactory; 
    
      import org.hibernate.cfg.AnnotationConfiguration; 
    
      import org.hibernate.classic.Session; 
    
      import org.junit.AfterClass; 
    
      import org.junit.BeforeClass; 
    
      import org.junit.Test; 
    
      public class TeacherTest { 
    
      public static SessionFactory sessionFactory = null; 
    
      @BeforeClass 
    
      public static void beforeClass() { 
    
      try { 
    
      sessionFactory = new AnnotationConfiguration().configure().buildSession Factory(); 
    
      } catch (HibernateException e) { 
    
      e.printStackTrace(); 
    
      } 
    
      } 
    
      @Test 
    
      public void testTeacherSave() { 
    
      Teacher teacher = new Teacher(); 
    
      teacher.setName("martian"); 
    
      teacher.setAge(23); 
    
      Session session = sessionFactory.getCurrentSession(); 
    
      session.beginTransaction(); 
    
      session.save(teacher); 
    
      //测试getCurrentSession()方法,这里输出true,因为在一个事务内,所以取得线程中的session 
    
      Session session1 = sessionFactory.getCurrentSession(); 
    
      System.out.println(session == session1); 
    
      //commit()之后不用close(),假如使用sessionFactory.openSession();就需要close(); 
    
      session.getTransaction().commit(); 
    
      //测试getCurrentSession()方法,这输出false,上一个事务已经提交,这里将重新生成一个session 
    
      Session session2 = sessionFactory.getCurrentSession(); 
    
      System.out.println(session == session2); 
    
      } 
    
      @AfterClass 
    
      public static void afterClass() { 
    
      sessionFactory.close(); 
    
      } 
    
      } 
    
  • 相关阅读:
    Spark Locality Sensitive Hashing (LSH)局部哈希敏感
    Spark ChiSqSelector 卡方选择器
    图解开源协议
    如何使用Hasu USB to USB Controller Converter刷写tmk固件交换Caps和Ctrl
    使用PHP读取PHP文件并输出到屏幕上
    Mac修改显示器使支持原生缩放
    PHP中使用PDO的预处理功能避免SQL注入
    如何做数据库分页查询
    Chrome报错提示Unchecked runtime.lastError: The message port closed before a response was received.
    Parallels Desktop虚拟机无法关机提示“虚拟机处理器已被操作系统重置”
  • 原文地址:https://www.cnblogs.com/Laupaul/p/2344414.html
Copyright © 2020-2023  润新知