NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

我在启动 HibernateJPAVendorAdapter 时遇到了 NoClassDefFoundError

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [META-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

我检查了几个关于此异常的线程,但未能找出其根本原因。我认为 JPAVendorAdapter 的初始化失败但不确定原因。也许我的包依赖项之间不兼容,或者在我的 applicationContext.xml 中不兼容。他们来了; pom.xml(仅包含相关部分)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.canreef</groupId>
    <artifactId>Calendar</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>Calendar</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>

        <org.springframework.version>4.0.1.RELEASE</org.springframework.version>
        <org.springframework.data.version>1.7.0.RELEASE</org.springframework.data.version>
        <org.springframework.instrument.version>4.1.1.RELEASE</org.springframework.instrument.version>
        <org.hibernate.version>4.1.7.Final</org.hibernate.version>
        <jackson.version>2.4.4</jackson.version>
        <external.resources>${project.basedir}/src/main/lib</external.resources>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>        
        <!-- Added-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency> 
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${org.springframework.data.version}</version>

        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${org.springframework.instrument.version}</version>
            <scope>provided</scope>
        </dependency>        
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.11.1.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${external.resources}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
</project>

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd">

    <jpa:repositories base-package="com.canreef.calendar"/>
    <context:component-scan base-package="com.canreef.calendar"/>
    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="url" value="jdbc:derby://localhost:1527/GEAH"/>
        <property name="username" value="xxx"/>
        <property name="password" value="xxx"/>
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="Test_PU"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="packagesToScan" value="com.canreef.calendar"/>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
</beans>

如果有人能指出我的代码中的错误,我将不胜感激。

NoClassDefFoundError with Spring maven hibernate org/hibernate/ejb/HibernatePersistence 相同的问题? 你错过了一个依赖:

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>${hibernate.version}</version>
</dependency>

如果您遇到 NoClassDefFound 异常,一个好的起点是查看缺少的 class 是否在您的依赖项 jar 之一中。如果没有,请添加缺少的依赖项。

springframework-orm POM 指的是 hibernate-entitymanager 4.2.8.Final(参见 http://central.maven.org/maven2/org/springframework/spring-orm/4.0.1.RELEASE/spring-orm-4.0.1.RELEASE.pom)您依赖 4.1.*。

请确保您编译的 war 文件只包含一个 org.hibernate.javax.persistencejar,它应该是 2.1。 Spring-data-jpa 拖拽 hibernate-jpa 2.0,你应该在你的 pom.xml.

中明确排除它

最重要的是,您确实需要删除 hibernate-core,并且在您的 class 路径上只包含 hibernate-entitymanager。确保没有其他东西拖累 hibernate-core。您要查找的 class 仅包含在 hibernate-entitymanager 中。

这两件事绝对是你的罪魁祸首。编译并检查正确的 JAR。如果您从 Eclipse 中启动 Tomcat,请确保您的 Deployment Assembly 中包含所有正确的 class 路径文件。这样做 mvn eclipse:eclipse 也可能有所帮助。

基本上,在服务器启动之前,确保所有 jars/classes 都在它们应该在的位置。按照我说的去做应该已经解决了它,但如果没有,它肯定有助于找出罪魁祸首。