`
itmyhome
  • 浏览: 95532 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate之openSession和getCurrentSession

 
阅读更多

hibernate通过SessionFactory有两种方式获取session: 一种是openSession 一种是getCurrentSession

区别:

a)openSession创建session时autoClassSessionEnabled参数为false,即在事务结束后不会自动关闭session 需要手动关闭

b)getCurrentSession创建session时autoCloseSessionEnabled,flushBeforeCompletionEnabled都为true 事务结束后自动关闭session

如果使用getCurrentSession获取session时 需要在hibernate.cfg.xml中加入如下配置

本地事务

<property name="hibernate.current_session_context_class">thread</property>


全局事务

<property name="hibernate.current_session_context_class">jta</property>


不然的话会报如下错误:

org.hibernate.HibernateException: No CurrentSessionContext configured!
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:620)
	at com.itmyhome.model.StudentTest.test(StudentTest.java:26)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)  .........


分享到:
评论

相关推荐

    SessionFactory.getCurrentSession与openSession的区别

    博文链接:https://shaqiang32.iteye.com/blog/201918

    getCurrentSession 与 openSession() 的区别

    NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085

    hibernate 学习笔记

    hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate

    Hibernate3使用经验

    ---------------Hibernate3.0 配置-------------- 1.Hibernate中配置参数 /** * 注意:HQL中使用参数的方法: * 1.根据参数名称来设置参数:匹配名称; * 2....Session session = sf.openSession();

    hibernate操作数据库笔记

    //该方法将到classpath下解析hibernate.cfg.xml中的配置,如果不用Hibernate默认的配置文件名和路径,可在该方法中指定Hibernate配置文件的名称和路径 2.用Configuration对象获取SessionFactory和Session对象:...

    hibernate实现分页查询

    hibernate 分页查询的实现 hibernate 内置的有分页功能 有三个参数 thisnumber一个是当前页数 sumcount是一页显示多少条数据 sql是用来查询的sql语句 public List ... Session session = sf.openSession();

    SSH-Jsp期末考试简答题带答案

    openSession()和getCurrentSession()是Hibernate中两个重要的Session创建方法。采用getCurrentSession()创建的Session会绑定到当前的线程中去,而采用openSession()则不会。getCurrentSession()创建的Session在...

    Spring4.0+Hibernate4.0+Struts2.3整合案例

    Spring4.0+Hibernate4.0+Struts2.3整合案例:实现增删改查。... 可以解决办法是:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!

    struts2.3.x+spring3.1.x+hibernate3.6 demo

    关键问题有几个,第一个HibernateDaoSupport这个没有了,在使用hibernateTemplate的时候,报错误:java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session 很是悲...

    Hibernate1

    6.创建映射文件(文件名为Student.hbm.xml,一般我们将其方法和实体类同一目录下面) &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" ...

    J2EE利用Hibernate采用B/S架构网页设计

    sessionFactory.openSession() : null; threadLocal.set(session); } return session; } /** * Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { ...

    hibernate精华教程

    (1)Configuration:用于解析hibernate.cfg.xml文件和XXXXX.hbm.xml文件,并创建SessionFactory对象。Configuration对象用于配置并且启动Hibernate。Hibernate应用通过Configuration实例来指定对象--关系映射文件的...

    hibernate经典文档

    hibernate 经典文档,学习hibernate 必备的文档,深入浅出,非常实用,强烈推荐!

    Java面试宝典2020修订版V1.0.1.doc

    9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...

    Hibernate3.2.6ga 支持Session修改Schema

    修改Hibernate3.2.6ga可以动态通过Session设置Schema Session session=sf.openSession(); session.setSchema("SchemaName"); session.save........ ....... .......

    hibernate session.doc

    例如以下代码先加载一个持久化对象,然后通过delete()方法将它删除: Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer ...

    Java常见面试题208道.docx

    123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...

    Hibernate查询语言

    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...

Global site tag (gtag.js) - Google Analytics