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

xiahn 2012-12-16 02:45:06
大家好,我刚接触hibernate。一开始没有在web环境下,一切正常。但是当在web环境下使用时,总遇到java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration。

我在web.xml中添加了:
<listener>
<listener-class>com.mypackage.configuration.HibernateListener</listener-class>
</listener>
其中,com.mypackage.configuration.HibernateListener为:
package com.mypackage.configuration;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
* @author This class garanties that only one single SessionFactory
* is instanciated and that the configuration is done thread safe as
* singleton. Actually it only wraps the Hibernate SessionFactory.
* You are free to use any kind of JTA or Thread transactionFactories.
*/
public class HibernateSessionFactoryUtil {

/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;

/**
* disable contructor to guaranty a single instance
*/
private HibernateSessionFactoryUtil() {
}

static{
// Annotation and XML
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
// XML only
// sessionFactory = new Configuration().configure().buildSessionFactory();
}

public static SessionFactory getInstance() {
return sessionFactory;
}

/**
* Opens a session and will not bind it to a session context
* @return the session
*/
public Session openSession() {
return sessionFactory.openSession();
}

/**
* Returns a session from the session context. If there is no session in the context it opens a session,
* stores it in the context and returns it.
* This factory is intended to be used with a hibernate.cfg.xml
* including the following property <property
* name="current_session_context_class">thread</property> This would return
* the current open session or if this does not exist, will create a new
* session
*
* @return the session
*/
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}

/**
* closes the session factory
*/
public static void close(){
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;

}
}

在tomcat下运行后,始终报错:
java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration
at com.mypacakge.configuration.HibernateSessionFactoryUtil.<clinit>(HibernateSessionFactoryUtil.java:26)
at com.mypacakge.configuration.HibernateListener.contextInitialized(HibernateListener.java:8)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
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: java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 12 more

hibernate所有的jar我都load了 (下载的是:http://sourceforge.net/projects/hibernate/files/hibernate4/4.1.9.Final/)。请问是怎么回事呢?
...全文
694 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gokeiryou263 2013-10-23
  • 打赏
  • 举报
回复
缺少hibernate3.jar,引入即可
xingfuzhuzi1314 2012-12-16
  • 打赏
  • 举报
回复
LZ,你把jar放那儿了啊,呵呵!
  • 打赏
  • 举报
回复
xiahn 2012-12-16
  • 打赏
  • 举报
回复
找到原因了,是因为我没有将jars直接放在WEB-INF/lib下

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧