`
endual
  • 浏览: 3507441 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hibernage getcurrentsession

 
阅读更多

 

org.hibernate.HibernateException: No CurrentSessionContext configured

分类: Java(J2EE) 22人阅读 评论 (0) 收藏 举报
今天在测试的时候遇到了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  org.huawei.hibernate.junit;  
  2. import  java.util.Date;  
  3. import  org.hibernate.Session;  
  4. import  org.hibernate.SessionFactory;  
  5. import  org.hibernate.cfg.AnnotationConfiguration;  
  6. import  org.huawei.hibernate.Teacher;  
  7. import  org.huawei.hibernate.zhicheng;  
  8. import  org.junit.AfterClass;  
  9. import  org.junit.BeforeClass;  
  10. import  org.junit.Test;  
  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  SaveTest {  
  21.     private  static  SessionFactory sessionFactory;  
  22.       
  23.     @BeforeClass   
  24.     public  static  void  beforeClass() {  
  25.             sessionFactory = new  AnnotationConfiguration().configure().buildSessionFactory();  
  26.     }  
  27.     @AfterClass   
  28.     public  static  void  afterClass() {  
  29.         sessionFactory.close();  
  30.     }  
  31.     @Test   
  32.     public  void  testSaveWith3State() {  
  33.       
  34.         Teacher t = new  Teacher();  
  35.           
  36.         t.setName("何枫" );  
  37.         t.setTitle("middle" );  
  38.         t.setZhicheng(zhicheng.A);  
  39.         //t.setBirthDate(new Date());   
  40.         t.setDate(new  Date());  
  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  main(String[] args) {  
  64.         beforeClass();  
  65.     }  
  66. }  

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

相关推荐

    ssh中getCurrentSession的使用

    ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载

    新Hibernate SessionFactory().getCurrentSession()猫腻

    NULL 博文链接:https://zgdkik.iteye.com/blog/1835667

    getCurrentSession 与 openSession() 的区别

    NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085

    SessionFactory.getCurrentSession与openSession的区别

    博文链接:https://shaqiang32.iteye.com/blog/201918

    java 调用存储过程列子

    学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下

    OA项目SSH整合框架

    sessionFactory.getCurrentSession().save(new User()); // int a = 1 / 0; // 这行会抛异常 sessionFactory.getCurrentSession().save(new User()); } } 2,单元测试 @Test // ...

    NHibernate Demo

    .GetCurrentSession();不带参数的情况下Web.config中一定要有hibernate-configuration节 5.连接是单线程的,在整个httpRequest处理过程中是公用的,缓存在HttpContext context中 see ref NHibernateHelper.getSession....

    hibernate 学习笔记

    hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate

    basic-common2h3:基于hibernate3的BaseDao,完成增删改查、分页操作

    通过getCurrentSession方法获取session操作对象。 示例: public interface IUserDao extends IBaseDao { } 3、定义UserDao并实现IUserDao接口,且要实现BaseDao类还需要传入泛型。 BaseDao完成了具体的CRUD的方法。...

    mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试

    Session session1 =sessionFactory.getCurrentSession(); session1.beginTransaction(); &lt;br&gt; String title="jkh" ; Date theDate= new Date(); Event theEvent = new Event(); theEvent....

    对DAO编写单元测试[4]

    编写对DAO编写单元测试[4]publicabstractclassTransactionCallback{publicfinalObjectexecute()throwsException{Transactiontx=HibernateUtil.getCurrentSession().beginTransaction();try{Objectr=doInTransaction...

    三大框架下分页源代码

    Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql); query.setFirstResult(pageno); query.setMaxResults(3); //设置每页显示的条数 List result = ...

    Hibernate 修改数据的实例详解

    Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String hqlString = update Person p set p.name=? , p.age=? where p.id=?; //构建 Query Query ...

    Hibernate5的Query接口浅析

    Query接口的基本知识 使用Query对象可以方便的查询数据库中的数据,它主要使用HQL或者本地SQL查询数据。Query对象不仅能查询...Session session=sf.getCurrentSession(); Transaction tx=session.beginTransaction();

    Java面试宝典2020修订版V1.0.1.doc

    9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...

    无垠式代码生成器最新功能与文档增强版0.7.22

    3)增强修复S2SH技术栈,不再使用openSession语句,而是使用getSessionFactory().getCurrentSession(), Spring 3,Spring 4通用,同时web.xml里增加SpringOpenSessionInViewFilter ===============0.7.19.2==========...

Global site tag (gtag.js) - Google Analytics