java.lang.NoClassDefFoundError:org / hibernate / cfg / AnnotationConfiguration

时间:2014-03-24 17:09:55

标签: java hibernate

我已经创建了一个Web动态项目,并希望使用Hibernate

然后,我使用Hibernate Code Generation生成类代码,并创建了一个类HibernateUtil,我在其中初始化SessionFactory

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory(){
        try{
            SessionFactory sessionFactory = 
                    new AnnotationConfiguration().configure().buildSessionFactory();
            return sessionFactory;
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

另一个班级HibernateSessionFactoryListener

public class HibernateSessionFactoryListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        System.out.println("\n Context Initilaise \n");
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        HibernateUtil.shutdown();
        System.out.println("\n Context Detruit \n");
    }

}

当我运行我的项目时,我收到以下错误消息:

java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration
    at com.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:28)
    at com.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:11)
    at com.hibernate.HibernateSessionFactoryListener.contextInitialized(HibernateSessionFactoryListener.java:12)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    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.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    ... 12 more

我该如何解决这个问题?

编辑1:

这是我的Java构建路径:

enter image description here

1 个答案:

答案 0 :(得分:11)

在Hibernate 3.6中,不推荐使用“org.hibernate.cfg.AnnotationConfiguration”,并且其所有功能都已移至“org.hibernate.cfg.Configuration”。 因此,您可以使用“配置”类

安全地替换“AnnotationConfiguration”

来源:http://www.mkyong.com/hibernate/hibernate-the-type-annotationconfiguration-is-deprecated/