错误:org.hibernate.HibernateException: No CurrentSessionContext configured!
2012-11-21 13:06
453 查看
在使用Hibernate的时候出现的错误,代码如下
使用JUnite测试的时候报错,代码如下:
报错如下:
org.hibernate.HibernateException: No CurrentSessionContext configured!
解决方法:
在集成Hibernate的环境下,要在hibernate.cfg.xml中session-factory段加入:
Xml代码
<property name="current_session_context_class">jta</property>
在不集成Hibernate的环境下(例如使用JDBC的独立应用程序),在hibernate.cfg.xml中session-factory段加入:
Xml代码
<property name="current_session_context_class">thread</property>
public boolean exists(User u){ SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.getCurrentSession(); s.beginTransaction(); Query q = s.createQuery("select count(*) from User u where u.username = ?"); q.setString(0, u.getUsername()); long count =(Long) q.uniqueResult(); s.getTransaction().commit(); if(count > 0) return true; return false; }
使用JUnite测试的时候报错,代码如下:
public void testExists() { UserManager um = new UserManager(); User u = new User(); u.setUsername("a"); boolean exists = um.exists(u); Assert.assertEquals(true, exists); }
报错如下:
org.hibernate.HibernateException: No CurrentSessionContext configured!
解决方法:
在集成Hibernate的环境下,要在hibernate.cfg.xml中session-factory段加入:
Xml代码
<property name="current_session_context_class">jta</property>
在不集成Hibernate的环境下(例如使用JDBC的独立应用程序),在hibernate.cfg.xml中session-factory段加入:
Xml代码
<property name="current_session_context_class">thread</property>
相关文章推荐
- 错误:org.hibernate.HibernateException: No CurrentSessionContext configured!
- 小白日记_错误:org.hibernate.HibernateException: No CurrentSessionContext configured!
- 错误:org.hibernate.HibernateException:No CurrentSessionContext configured!
- Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
- Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
- 异常之org.hibernate.HibernateException: No CurrentSessionContext configured
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- 打开session的方法和org.hibernate.HibernateException: No CurrentSessionContext configured! 异常
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured
- Hibernate(三): org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- hibernate异常org.hibernate.HibernateException: No CurrentSessionContext configured!
- Hibernate3.6:org.hibernate.HibernateException: No CurrentSessionContext configured!