gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:39:11 24 4
gpt4 key购买 nike

我已经创建了一个网络动态项目,并且想使用 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

最佳答案

在 Hibernate 3.6 中,“org.hibernate.cfg.AnnotationConfiguration”已被弃用,其所有功能已移至“org.hibernate.cfg.Configuration”。因此,您可以安全地将“AnnotationConfiguration”替换为“Configuration”类

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

关于java.lang.NoClassDefFoundError : org/hibernate/cfg/AnnotationConfiguration 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com