简体   繁体   中英

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

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.

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!

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

If you do use eclipse, you can open the pom.xml in eclipse and navigate to the dependency heirarchy tab, to verify which jar is picked up as a transitive dependency and which is the one which you have configured. You would have to choose the one which is the transitive dependency and remove the current specified dependency / OR / you would have to exclude the transitive dependency.

在此输入图像描述

I used

compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'

to sovle it.

We faced the same problem. We have a web app using Spring and Hibernate 4.3.10.FINAL with JBoss 7.1.3. The Hibernate comes with JPA 2.1 and the JBoss with JPA 2.0. The two were in conflict when deploying the app. You have many solutions. As said by others, you can put the version of hibernate-jpa jar you want in the modules directory of jboss (jboss/modules/javax/persistence/api/main), and update the modules.xml with the good version. Another solution (the one we choosed) is to prevent the Jboss JPA to be implicitly loaded by JBoss when deploying. In this case, you have to create a file named jboss-deployment-structure.xml in your WEB-INF folder.And add the exclusions like below:

<?xml version="1.0" encoding="UTF-8"?>
<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>

After running into same problems with JBoss EAP 6.4, JPA 2.1, Spring Boot 1.4.1, Spring Data 1.10.3, Hibernate 5.0.11, this is the solution that worked for me:

/WEB-INF/jboss-deployment-structure.xml

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

Kudos to Ahmad's answer, only added exclusion for org.jboss.logging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM