4

I met the NoClassDefFoundError when initiating the HibernateJPAVendorAdapter

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)

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

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

OrlandoL
  • 898
  • 2
  • 12
  • 32
  • Why are you using hibernate-core AND hibernate-entitymanager? You should decide for one of the two. I also recommend you check if you have a dependency for `org.hibernate.javax.persistence` (preferably for JPA 2.1, which doesn't come with spring-data-jpa. – Schaka Jun 05 '15 at 07:30
  • @Schaka I tried removing hibernate-core but it didn't work out. Neither did adding hibernate.javax.persistence api 2.1. Also please note I already have hibernate.javax.persistence api 2.1. in pom – OrlandoL Jun 06 '15 at 06:25

2 Answers2

4

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.

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.

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.

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.

Schaka
  • 772
  • 9
  • 20
  • Thanks Schaka for the incisive answer. The culprit's been shot in the head and the incompatibility seems to be gone! – OrlandoL Jun 07 '15 at 07:26
  • 1
    To understand more about the included dependencies of your dependencies try ""mvn dependency:tree"" It helped me alot to understand why I have double jars with different versions. – Conffusion Jun 10 '15 at 07:20
2

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

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

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

Community
  • 1
  • 1
Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • No, it's the first dependency in the POM. This isn't the problem. – dunni Jun 05 '15 at 07:22
  • You're right. Sorry. Is there a specific reason why you use 2 versions for your hibernate dependencies (4.3.0 and 4.3.1) ? In the springframework-orm POM it 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 – Conffusion Jun 05 '15 at 07:34
  • Thanks Conffusion you made a very good point. The thing is the public repository in the company has some versions missing for the jars and this was why I used 4.3.0 and 4.3.1. I've managed to changed the 4.3.0 version to 4.2.8 but the exception persists. – OrlandoL Jun 05 '15 at 08:00