hibernate没有配置currentSessionContext - log4j有错吗?

时间:2014-01-06 18:43:09

标签: hibernate

我正在尝试进行保存操作。这是日志。

我已经尝试过为此找到一个依赖项,但似乎Hibernate使用log4j并且我的libs文件夹中有1.2.17版本。是否有一种易于解决的变通方法或解决方案?

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Jan 06, 2014 12:39:29 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [PersonServlet] in context with path [/SchedulingAutomation]     threw exception
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:698)
at dao.DaoFactory.getCurrentSession(DaoFactory.java:76)
at dao.DaoFactory.getDaoByClass(DaoFactory.java:57)
at dao.DaoFactory.getPersonDao(DaoFactory.java:33)
at controller.PersonServlet.save(PersonServlet.java:73)
at controller.PersonServlet.doGet(PersonServlet.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

编辑: 以下是与hibernate函数相关的类:

// save the person
DaoFactory.getPersonDao().save(person);

// returns a Person DAO
public static PersonDao getPersonDao(){
    System.out.println("getting person dao");
    return (PersonDao)getDaoByClass(PersonDao.class);
}

/* 
     * returns the Dao and configures it with the 
     * current session
     */
    public static AbstractDao getDaoByClass(Class c)
    {
        System.out.println("getting dao by class");
        try
        {
            Session s = getCurrentSession(); <--error here
            AbstractDao dao = (AbstractDao)c.newInstance();
            dao.setSession(s);
            System.out.println("success");
            return dao;
        }
        catch (InstantiationException ex) {
            ex.printStackTrace();
        }
        catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        System.out.println("failed");

public static Session getCurrentSession()
{
    System.out.println("getting hibernate session");
    return HibernateUtil.getSessionFactory().getCurrentSession();
}


// part of the abstractDao that the person dao extends
public void save(Object o){
        session.saveOrUpdate(o);
    }

Hibernate配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">nbuser</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/scheduling1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
    </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

在此跟踪中似乎有2个问题。

第一个问题(仅警告):缺少log4j配置

在类路径中添加log4j.properties文件(通常src / main / resources是个好位置)。以下是此log4j.properties的示例内容:

log4j.rootLogger = ALL, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.conversionPattern=%m%n 

第二个问题(SEVERE错误):你无法获得一个hibernate会话的实例。

为了帮助您:您必须发布有关您的hibernate(和事务)配置以及抛出异常的代码(DaoFactory)的更多信息