您的位置:首页 > 其它

错误:org.hibernate.HibernateException: No CurrentSessionContext configured!

2012-11-21 13:06 453 查看
在使用Hibernate的时候出现的错误,代码如下

 

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>  

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐