如何在调用servlet之前静态初始化HibernateUtil?

时间:2014-02-10 21:29:20

标签: java hibernate tomcat

我希望java servlet在doGet方法中通过hibernate访问数据库,我创建了HibernateUtil类以在静态上下文中调用buildSessionFactory

测试时,我收到错误“tomcat启动失败”,显然是(?)因为doGet方法中的openSession是在HibernateUtil类中调用buildSessionFactory之前调用的。

如何确保在servlet中的doGet方法之前对HibernateUtil类进行静态初始化?

这是班级:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration()
                .configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

我正在调用doGet()方法,如下所示:

Session session = null;
try {
    session = HibernateUtil.getSessionFactory().openSession();
    // ...
}

堆栈跟踪如下:

INFO: Starting service Catalina
Feb 10, 2014 12:58:21 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.42
Feb 10, 2014 12:58:21 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/dbwebtest]]
  at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
  at java.util.concurrent.FutureTask.get(Unknown Source)
  at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
  at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
  at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
  at java.util.concurrent.FutureTask.run(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/dbwebtest]]
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
  ... 7 more
Caused by: java.lang.NoClassDefFoundError: org/hibernate/HibernateException
  at java.lang.Class.getDeclaredFields0(Native Method)
  at java.lang.Class.privateGetDeclaredFields(Unknown Source)
  at java.lang.Class.getDeclaredFields(Unknown Source)
  at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
  at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:263)
  at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:142)
  at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:67)
  at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:405)
  at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:881)
  at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
  at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
  at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  ... 7 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.HibernateException
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
  ... 21 more

2 个答案:

答案 0 :(得分:1)

看起来hibernate jar文件(或其中一个依赖项)不在webapp的类路径中,即在WEB-INF / lib中:

  

java.lang.NoClassDefFoundError:org / hibernate / HibernateException

答案 1 :(得分:0)

您应该使用Servlet过滤器来创建SessionFactory并打开/关闭会话。 Hibernate将此称为Open-Session-In-View模式,并在此处描述了实现:https://community.jboss.org/wiki/OpenSessionInView?_sscc=t