getCurrentSession () 使用当前的 session
openSession() 重新建立一个新的 session
-------------------------
SessionFactory.getCurrentSession() 是 Hibernate 应用获取 Session 的常用方法。在调用该方法时, Hibernate 会从 interface CurrentSessionContext 获取当前的 Session ,这是 Hibernate 在不同组件中传递 Session 的方法。
CurrentSessionContext 有三个实现,分别是 ThreadLocalSessionContext 、 JTASessionContext 和 ManagedSessionContext 。
ThreadLocalSessionContext 将 Session 与当前线程绑定,是使用较多的一种方案;
JTASessionContext 将 Session 与 JTA 事务绑定,在 JTA 环境中使用;
ManagedSessionContext 使应用可以通过 bind() 和 unbind() 方法控制 Session 的绑定,主要在有 Conversation 的应用中使用(如果使用 ManagedSessionContext ,开发人员要做的事情还是很多的)。
CurrentSessionContext 实现的选择可以通过 hibernate.current_session_context_class 来配置。
另一种更常见的创建 Session 的方法是 openSession() 。
openSession() 与 getCurrentSession() 有何不同和关联呢?
在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
再说 ManagedSessionContext ,它提供了更灵活的绑定 Session 的方式,但是使用起来去不简单。
在 Hibernate 的 CaveatEmptor 实例中有关于使用 ManagedSessionContext 的例子,但更好的选择是使用 Seam Framework 。
分享到:
相关推荐
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
MyEclipse生成的HibernateSessionFactory.java文件,应该是完整的。
Session session=sessionFactory.openSession(); String hql="from User as u where u.username=? and u.userpass=? and u.userright=?"; Query query=session.createQuery(hql) ; query.setString(0, u....
Session s= HibernateSessionFactory.getSession(); 就是Hibernate的工具java类
4.sessionFactory.openSession();//打开Sesssion 5.session.beginTransaction();//创建事务Transation 6.persistent operate持久化操作 7.session.getTransaction().commit();//提交事务 8.关闭Session 9.关闭...
Session session = sessionFactory.openSession(); 或者通过MyEclipse的Hibernate工具自动生成的HibernateSessionFactory.getSession()方法来获取Session,调用HibernateSessionFactory. closeSession()方法来关闭...
关键问题有几个,第一个HibernateDaoSupport这个没有了,在使用hibernateTemplate的时候,报错误:java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session 很是悲...
session=sessionfactory.openSession(); tran=session.beginTransaction(); //Users user=(Users)session.get(Users.class, 1001);
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0...
SSH项目改为Spingboot项目,将项目中部分需要调用存储过程的部分用entityManagerFactory.unwrap(SessionFactory.class).openSession()来获取Session实现后发现项目访问数据库超过十次就会挂掉,原因是Springboot...
例如以下代码先加载一个持久化对象,然后通过delete()方法将它删除: Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer ...
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for (int i=0; i; i++) { Customer customer = new Customer(...); session.save(customer); if (i % 20 == 0) ...
NULL 博文链接:https://chaoyi.iteye.com/blog/2148991
4.sessionFactory.openSession();//打开Sesssion 5.session.beginTransaction();//创建事务Transation 6.persistent operate持久化操作 7.session.getTransaction().commit();//提交事务 8.关闭Session 9.关闭...