zoukankan      html  css  js  c++  java
  • No CurrentSessionContext configured! 的解决方法

    配置Hibernate时报下面的错误:

    log4j:WARN No appenders could be found for logger (org.jboss.logging).

    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:986)
    at test.Test.test(Test.java:15)
    at test.Test.main(Test.java:28)

    从网上找到的解决办法:

    以下内容来至网上资料

    1、用这种方法正确
    static {

    try {
    configuration = new Configuration();

    sessionFactory = configuration.configure().buildSessionFactory();
    } catch (Throwable ex) {

    throw new ExceptionInInitializerError(ex);
    }
    }

    2、这种方式会出现异常

    // static {
    // try {
    // // Create a configuration based on the properties file we've put
    // Configuration config = new Configuration();
    // config.addClass(Customer.class).addClass(Order.class);
    // // Get the session factory we can use for persistence
    // sessionFactory = config.buildSessionFactory();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }

    第二种方式是针对使用properties文件配置hiernate的写法,使用hibernate.cfg.xml应使用第一种调用方式

    或者在hibernate.cfg.xml中加入:

    <property name="current_session_context_class">thread</property>
     
    自己的解决方法是在hibernate.cfg.xml中添加<property name="current_session_context_class">thread</property> 这句。
  • 相关阅读:
    java hashmap 缓存
    android 界面刷新 post send
    android 五种 布局文件
    通过枚举窗口,实现最小化到托盘中程序的窗口显示
    转 String,CString,TCHAR*,char*之间区别和联系
    标准C++ 时间日期函数
    话说程序员的职业生涯
    李开复:创业必备
    职业化的软件工程师
    让高版本ie兼容低版本
  • 原文地址:https://www.cnblogs.com/szlwork/p/3332995.html
Copyright © 2011-2022 走看看