0
点赞
收藏
分享

微信扫一扫

hibernage getcurrentsession

yundejia 2023-05-10 阅读 75


 

org.hibernate.HibernateException: No CurrentSessionContext configured



今天在测试的时候遇到了org.hibernate.HibernateException: No CurrentSessionContext configured!错误。。。找了半天,在百度上才发现是什么原因。。。 
初学hibernate,出现No CurrentSessionContext configured错误,检查了几次配置都没有发现问题,最后上网查找发现是配置文件的问题 
解决如下: 
    在集成Hibernate的环境下(例如Jboss),在hibernate.cfg.xml中session-factory段加入: 
<property name="current_session_context_class">jta</property>  
    在不集成Hibernate的环境下(例如使用JDBC的独立应用程序),在hibernate.cfg.xml中session-factory段加入: 
<property name="current_session_context_class">thread</property>  
加入了这句话什么测试类就可以运行了。。。。哎,这个错误我来记录下 

Java代码  

 
    
  
1. package
2. import
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11.   
12. /**
13.  * @作者: 何枫
14.  * @日期: 2011-4-10下午11:57:04
15.  * @版本: V 1.0 
16.  * @标题: SaveTest.java
17.  * @包名: org.huawei.hibernate.junit
18.  * @描述: TODO(用一句话描述该文件做什么)
19.  */
20. public  class
21. private  static
22.       
23. @BeforeClass
24. public  static  void
25. new
26.     }  
27. @AfterClass
28. public  static  void
29.         sessionFactory.close();  
30.     }  
31. @Test
32. public  void
33.       
34. new
35.           
36. "何枫"
37. "middle"
38.         t.setZhicheng(zhicheng.A);  
39. //t.setBirthDate(new Date());
40. new
41. //Session session = sessionFactory.openSession();
42. /*
43.          * 在使用sessionFactory.getCurrentSession()是必须在hibernate的配置文件中加入
44.          * <property name="current_session_context_class">thread</property> 
45.          */
46. /**
47.          * openSession与getCurrentSession的区别是:
48.          * openSession每次都是新的,需要close
49.          * getCurrentSession从上下文找,如果有,用旧的,如果没有,建新的
50.          *  1.用途,界定事务边界
51.          *  2.事务提交自动close
52.          */
53.         Session session = sessionFactory.getCurrentSession();  
54.         session.beginTransaction();  
55.         session.save(t);  
56.         System.out.println(t.getId());  
57.         session.getTransaction().commit();  
58. //session.clear();
59.         System.out.println(t.getId());  
60.     }  
61.       
62.        
63. public  static  void
64.         beforeClass();  
65.     }  
66. }


终于搞定了.学习软件开发是需要日积月累的。。。。更应正了一句最经典的话,,程序员是在错误中长大的! 

举报

相关推荐

0 条评论