java NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30660527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:27:29  来源:igfitidea点击:

NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence

javaspringhibernatemavenspring-data-jpa

提问by OrlandoL

I met the NoClassDefFoundError when initiating the HibernateJPAVendorAdapter

我在启动 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

I checked several threads on this exception but failed to find out the root cause of this. I think the initialization of JPAVendorAdapter has failed but not sure why. Maybe incompatibilities among my package dependencies, or in my applicationContext.xml. Here they are; pom.xml(only related sections included)

我检查了有关此异常的几个线程,但未能找出其根本原因。我认为 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>
        <Hymanson.version>2.4.4</Hymanson.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

<?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>

Really appreciate it if anybody can point out what is wrong in my code.

如果有人能指出我的代码有什么问题,真的很感激。

采纳答案by Schaka

Please make sure your compiled war file contains only ONE org.hibernate.javax.persistencejar, it should be 2.1. Spring-data-jpa drags hibernate-jpa 2.0 along with it, you should exclude that specifically in your pom.xml.

请确保您编译的 war 文件只包含一个org.hibernate.javax.persistencejar,它应该是 2.1。Spring-data-jpa 将 hibernate-jpa 2.0 与其一起拖动,您应该在 pom.xml 中专门排除它。

On top of that, you really need to remove hibernate-core and ONLY have hibernate-entitymanager on your class path. Make sure, nothing else drags along hibernate-core either. The class you're looking for is only contained within hibernate-entitymanager.

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

These 2 things are DEFINITELY your culprit. Compile and check for the correct JARs. If you're starting Tomcat from within Eclipse, make sure your Deployment Assemblyhas all the correct classpath files in it. Doing mvn eclipse:eclipsemight also help.

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

Basically, before the server starts, make sure all jars/classes are exactly where they should be. Doing as I said should fix it already, but if it doesn't, it should definitely help identity the culprit.

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

回答by Conffusion

Same question as NoClassDefFoundError with Spring maven hibernate org/hibernate/ejb/HibernatePersistence? You miss a dependency:

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

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

If you have a NoClassDefFound exception a good starting point is to look if the missing class is in one of your dependency jars. If not, add the missing dependency.

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

The springframework-orm POM refers to hibernate-entitymanager 4.2.8.Final (see http://central.maven.org/maven2/org/springframework/spring-orm/4.0.1.RELEASE/spring-orm-4.0.1.RELEASE.pom) You depend on 4.1.*.

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.*。