• 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> 这句。
  • 相关阅读:
    hdu 3496
    poj 2374
    zoj 3399
    poj 1321
    sgu 365
    hdu 3555
    poj 3345
    poj 2355
    Android重命名文件
    在workflow中传值的sample
  • 原文地址:https://www.cnblogs.com/szlwork/p/3332995.html
Copyright © 2020-2023  润新知