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) .........
分享到:
相关推荐
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
---------------Hibernate3.0 配置-------------- 1.Hibernate中配置参数 /** * 注意:HQL中使用参数的方法: * 1.根据参数名称来设置参数:匹配名称; * 2....Session session = sf.openSession();
//该方法将到classpath下解析hibernate.cfg.xml中的配置,如果不用Hibernate默认的配置文件名和路径,可在该方法中指定Hibernate配置文件的名称和路径 2.用Configuration对象获取SessionFactory和Session对象:...
hibernate 分页查询的实现 hibernate 内置的有分页功能 有三个参数 thisnumber一个是当前页数 sumcount是一页显示多少条数据 sql是用来查询的sql语句 public List ... Session session = sf.openSession();
openSession()和getCurrentSession()是Hibernate中两个重要的Session创建方法。采用getCurrentSession()创建的Session会绑定到当前的线程中去,而采用openSession()则不会。getCurrentSession()创建的Session在...
Spring4.0+Hibernate4.0+Struts2.3整合案例:实现增删改查。... 可以解决办法是:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!
关键问题有几个,第一个HibernateDaoSupport这个没有了,在使用hibernateTemplate的时候,报错误:java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session 很是悲...
6.创建映射文件(文件名为Student.hbm.xml,一般我们将其方法和实体类同一目录下面) <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" ...
sessionFactory.openSession() : null; threadLocal.set(session); } return session; } /** * Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { ...
(1)Configuration:用于解析hibernate.cfg.xml文件和XXXXX.hbm.xml文件,并创建SessionFactory对象。Configuration对象用于配置并且启动Hibernate。Hibernate应用通过Configuration实例来指定对象--关系映射文件的...
hibernate 经典文档,学习hibernate 必备的文档,深入浅出,非常实用,强烈推荐!
9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...
修改Hibernate3.2.6ga可以动态通过Session设置Schema Session session=sf.openSession(); session.setSchema("SchemaName"); session.save........ ....... .......
例如以下代码先加载一个持久化对象,然后通过delete()方法将它删除: Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer ...
123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...
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...