JUit报错:org.hibernate.HibernateException: No CurrentSessionContext configured!hibernate.cfg.xml配置文件已经配置了:<property name="hibernate.current_session_context_class">thread</property>测试文件:package Student;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Test;public class SessionTest { @Test public void testopenSession() { //1.创建配置对象 Configuration config=new Configuration().configure(); //2.创建服务配置对象 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder(). applySettings(config.getProperties()).buildServiceRegistry(); //3.创建会话工厂 SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); //4.打开会话 Session session=sessionFactory.openSession(); if(session!=null) { System.out.print("session创建成功"); }else{ System.out.print("session创建失败"); } } @Test public void testgetCurrentSession() { //1.创建配置对象 Configuration config=new Configuration().configure(); //2.创建服务配置对象 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder(). applySettings(config.getProperties()).buildServiceRegistry(); //3.创建会话工厂 SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); //4.获得会话 Session session2 =sessionFactory.getCurrentSession(); if(session2!=null) { System.out.print("session创建成功"); }else{ System.out.print("session创建失败"); } }} 求大神指点这里是哪里错误
2 回答
慕粉4075985
TA贡献104条经验 获得超33个赞
要用getCurrentSession();需要在hibernate.cfg.xml 配hibernate.current_session_context_class =thread
添加回答
举报
0/150
提交
取消