在将Spring 3.1与Hibernate 4.0.1一起使用时,如何解析java.lang.ClassNotFoundException:org.hibernate.util.DTDEntityResolver?

2022-09-01 08:10:40

我最近升级到Hibernate 4.0.1.Final,并在上下文启动时收到以下错误:

Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2823)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1160)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1655)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533)
    at org.hibernate.cfg.Configuration.reset(Configuration.java:322)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:261)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:265)
    at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:150)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:71)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 65 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.util.DTDEntityResolver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533)

我的配置如下:

<!-- Holding Properties for hibernate -->
    <context:property-placeholder location="classpath:hibernate.properties"/>

    <!-- Configure annotated beans -->
    <context:annotation-config />
    <context:component-scan base-package="com.mypackage" />

    <!-- Drives transactions using local JPA APIs -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory"/>

    <!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSource"
        p:packagesToScan="com.mypackage.entity"
        p:jpaVendorAdapter-ref="jpaAdapter"/>

    <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                p:showSql="true"
                p:generateDdl="false"
                p:database="MYSQL"
                p:databasePlatform="org.hibernate.dialect.MySQL5InnoDBDialect" />

    <!-- Deploys datasource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:${mysql.port}/?zeroDateTimeBehavior=convertToNull"
        p:username="bla"
        p:password="bla123"/>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

一些依赖项是:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>ejb3-persistence</artifactId>                    
            </exclusion>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>                  
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
            </exclusion>
            <exclusion>
                <groupId>jboss</groupId>
                <artifactId>javassist</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <properties>
        <hibernate.version>4.0.1.Final</hibernate.version>
    </properties>

和弹簧版本: 3.1.0.发布

我假设有些东西正在尝试读取xml文件。除了属性文件之外,没有用于休眠的休眠 xml 配置。持久性.xml当前设置中不存在。为什么或谁正在寻找一个在休眠 4.0.1 中不存在的类?

任何帮助将不胜感激!


答案 1

根据 Hibernate 注释 3.5 文档:*

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

您应该删除 对 的依赖关系,并从依赖关系中删除 s。通常,不应混合使用依赖包的版本。hibernate-annotationsexcludehibernate-entitymanager

*和JB Nizet的评论。


答案 2

将此依赖项添加到您的 pom 中.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>tck-utils-api</artifactId>
    <version>0.9.1</version>
</dependency>

推荐