Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4.3.0.Final & Spring Data JPA 1.4.3.RELEASE

I was trying to setup and use Spring Data for the first time. Naturally you would want to use the latest version(Spring Data JPA 1.4.3.RELEASE & Hibernate 4.3.0.Final). After configuring as per the examples online, the application threw an exception.

 <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-jpa</artifactId>
   <version>1.4.3.RELEASE</version>
 </dependency>
 <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>Hibernate 4.3.0.Final</version>
   <exclusions>
     <exclusion>
       <artifactId>commons-collections</artifactId>
       <groupId>commons-collections</groupId>
     </exclusion>
   </exclusions>
 </dependency>
 <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-search</artifactId>
   <version>4.4.2.Final</version>
   <exclusions>
     <exclusion>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
     </exclusion>
   </exclusions>
 </dependency>

This was the error / exception :

 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [test-service-persistance-application-context.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:121)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 31 more

 Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:781)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3762)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3716)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:150)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:67)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:318)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 46 more

I just could not manage to get this configuration to work. The only workaround I found was to revert Hibernate to version 4.2.8.Final

like image 246
user3201424 Avatar asked Jan 16 '14 08:01

user3201424


3 Answers

Your classpath apparently contains both the JPA 2.0 and the JPA 2.1 JAR with the former being found first and thus causing Hiberate to fail. From the dependency declarations you listed it's not clear why, as Spring Data JPA 1.4.3 definitely doesn't pull it in.

So I'd recommend to try mvn dependency:tree and have a look for the JPA 2.0 JAR and who actually transitively depends on it (maybe you even have declared it locally). In case you're still stuck with it, feel free to add the output of the Maven command to your question.

like image 115
Oliver Drotbohm Avatar answered Oct 26 '22 14:10

Oliver Drotbohm


If you use Jboss web server, you would like to exclude the hibernate-jpa-2.0-api from pom.xml, for example:

<dependency>
   <groupId>org.jboss.spec</groupId>
   <artifactId>jboss-javaee-6.0</artifactId>
   <version>1.0.0.Final</version>
   <type>pom</type>
   <scope>provided</scope>
   <exclusions>
      <exclusion>
         <groupId>org.hibernate.javax.persistence</groupId>
         <artifactId>hibernate-jpa-2.0-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>

And add hibernate-jpa-2.1-api dependency to your pom.xml, for example:

 <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
 </dependency>

Final Note: Read the stacktrace, your query is not causing the error, it is being caused when the EntityManagerFactory is created, because some of your entities are using this property in a @Table annotation. Either delete the usage of the property or move your libraries to JPA 2.1.

Also, you need to exclude the jpa subsystem from JBoss, and you would like to do like the following:

  1. Create /WEB-INF/jboss-deployment-structure.xml
  2. Exclude the jpa subsystem: here is a full example

    <?xml version="1.0"?>
    <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
       <deployment>
           <exclude-subsystems>
              <subsystem name="jpa" />
           </exclude-subsystems>
           <exclusions>
              <module name="javaee.api" />
           </exclusions>
      </deployment></jboss-deployment-structure>
    

-Enjoy!

like image 32
Ahmad AlMughrabi Avatar answered Oct 26 '22 12:10

Ahmad AlMughrabi


    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
like image 20
Slim BH Avatar answered Oct 26 '22 12:10

Slim BH