java.lang.NoClassDefFoundError:org / hibernate / util / DTDEntityResolver

时间:2013-10-10 13:10:16

标签: java spring hibernate servlets spring-mvc

运行时出错,我使用的是Spring MVC + Hibernate,Spring 3.2发行版和hibernate 4发行版。

如下所示,模型中的代码,servlet xml和所有库的列表都被添加到路径WEB-INF / lib。

1- FEP_Health_Model

package Models;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class FEP_Health_Model {
   @Autowired
   private SessionFactory factory;

   public FEP_Health_Model()
  { 
   System.out.print("Hello Spring MVC with Hibernate");
       try{
              configureSessionFactory();
   }catch(Throwable ex){
       System.err.println("Failed to create sessionFactory object." + ex); throw new    
               ExceptionInInitializerError(ex);
   }
  }
  @Transactional
  public void test()
  {
   Session session = factory.openSession();
   Transaction trans = null;
   try{
      trans = session.beginTransaction();
      /*
       Some Code 
      */
      trans.commit();
   }catch(Throwable ex){

   }finally{
       session.close();
   }
   }
   private void configureSessionFactory() throws HibernateException {
    AnnotationConfiguration configuration = new AnnotationConfiguration();
    configuration.configure().addAnnotatedClass(FepEnergyData.class);       
    factory = configuration.buildSessionFactory();
   }
}

2- springmvc-servlet.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

 <context:component-scan base-package="Controllers"></context:component-scan>
 <context:component-scan base-package="Models"></context:component-scan>
 <context:annotation-config />


 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/JSP/" />
    <property name="suffix" value=".jsp" />
 </bean>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
   <property name="url"     
   value="jdbc:sqlserver://127.0.0.1:1433;databaseName=AECMDMS_TEST;instanceName=SQLEXPRESS;"/>
   <property name="username" value="user"/>
   <property name ="password" value="123" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>/META-INF/hibernate.cfg.xml</value>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServer2008Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

   <bean id="transactionManager"   
     class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <tx:jta-transaction-manager transaction-manager="transactionManager"/>
</beans>

已添加所有库的3个列表

    commons-digester3-3.2.jar   
    commons-io-2.4.jar
    commons-logging-1.1.3.jar
    dom4j-1.6.1.jar
    ejb3-persistence.jar
    hibernate-annotations-3.5.6-Final.jar
    hibernate-commons-annotations-4.0.2.Final.jar
    hibernate-core-4.2.5.Final.jar
    hibernate-envers-4.2.5.Final.jar
    hibernate-jpa-2.0-api-1.0.1.Final.jar
    javassist-3.15.0-GA.jar
    jboss-logging-3.1.0.GA.jar
    jboss-transaction-api_1.1_spec-1.0.1.Final.jar
    log4j.jar
    org.osgi.core-4.3.1.jar
    org.springframework.aop-3.2.0.RELEASE.jar
    org.springframework.beans-3.2.0.RELEASE.jar
    org.springframework.context-3.2.0.RELEASE.jar
    org.springframework.core-3.2.0.RELEASE.jar
    org.springframework.jdbc-3.2.0.RELEASE.jar
    org.springframework.orm-3.2.0.RELEASE.jar
    org.springframework.transaction-3.2.0.RELEASE.jar
    org.springframework.web.servlet-3.2.0.RELEASE.jar
    org.springframework.web-3.2.0.RELEASE.jar
    spring-webmvc-3.2.0.RELEASE.jar
    spring-asm-3.2.0.M1.jar 
    spring-tx-3.2.0.RELEASE.jar
    slf4j-api-1.7.5.jar
    slf4j-log4j12-1.7.5.jar
    slf4j-simple-1.7.5.jar
    sqljdbc4.jar
    xalan-2.7.1.jar
    xercesImpl.jar
    xml-apis.jar

控制台

  SEVERE: StandardWrapper.Throwable
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name     
  'sessionFactory' defined in ServletContext resource [/WEB-INF/FEP_Health-servlet.xml]:    
  Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError:   
  org/hibernate/util/DTDEntityResolver

我希望有人找到我的问题的解决方案

2 个答案:

答案 0 :(得分:1)

根据Hibernate注释3.5文档:*

Hibernate 3.5及更高版本包含Hibernate Annotations。

你应该删除对hibernate-annotations的依赖,并且从hibernate-entitymanager依赖中删除排除。通常,您不应混合依赖包的版本。

这对我有用。

答案 1 :(得分:0)

有相同的错误消息。 从pom中删除对hibernate-annotations的依赖(如上面的Prem所述)解决了这个问题,Hibernate继续生成模式(在我的例子中)。