Hibernate 4.3.0.Final& Spring Data JPA 1.4.3.RELEASE

时间:2014-01-16 08:50:48

标签: java spring jpa spring-data spring-data-jpa

我第一次尝试设置和使用Spring Data。当然,您可能希望使用最新版本(Spring Data JPA 1.4.3.RELEASE& Hibernate 4.3.0.Final)。根据在线示例进行配置后,应用程序引发了异常。

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

这是错误/异常:

 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

我无法让这个配置工作。我找到的唯一解决方法是将Hibernate恢复到版本4.2.8.Final

7 个答案:

答案 0 :(得分:19)

您的类路径显然包含JPA 2.0和JPA 2.1 JAR,前者首先被发现,从而导致Hiberate失败。从您列出的依赖声明中,不清楚为什么,因为Spring Data JPA 1.4.3肯定不会将其拉入。

所以我建议尝试mvn dependency:tree并查看JPA 2.0 JAR以及谁实际上依赖它(也许你甚至已经在本地声明了它)。如果您仍然坚持使用它,请随意将Maven命令的输出添加到您的问题中。

答案 1 :(得分:2)

如果您使用Jboss Web服务器,您希望从pom.xml中排除hibernate-jpa-2.0-api,例如:

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

并将hibernate-jpa-2.1-api依赖项添加到您的pom.xml中,例如:

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

最后注意:读取堆栈跟踪,您的查询没有导致错误,它是在创建EntityManagerFactory时引起的,因为您的某些实体在@Table注释中使用了此属性。删除属性的用法或将库移动到JPA 2.1。

此外,您需要从JBoss中排除jpa子系统,并且您希望执行以下操作:

  1. 创建/WEB-INF/jboss-deployment-structure.xml
  2. 排除jpa子系统:这是一个完整的例子

    <?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>
    
  3. -Enjoy!

答案 2 :(得分:1)

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

答案 3 :(得分:0)

如果你确实使用了eclipse,你可以在eclipse中打开pom.xml并导航到依赖项heirarchy选项卡,以验证哪个jar被选为传递依赖项,哪个是你配置的jar。您必须选择一个传递依赖项并删除当前指定的依赖项/ OR /您必须排除传递依赖项。

enter image description here

答案 4 :(得分:0)

我用过

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

来解决它。

答案 5 :(得分:0)

我们遇到了同样的问题。我们有一个使用Spring和Hibernate 4.3.10.FINAL和JBoss 7.1.3的Web应用程序。 Hibernate附带JPA 2.1和带JPA 2.0的JBoss。在部署应用程序时,两者发生冲突。 你有很多解决方案。正如其他人所说,你可以将你想要的hibernate-jpa jar版本放在jboss的模块目录(jboss / modules / javax / persistence / api / main)中,并使用好的版本更新modules.xml。 另一个解决方案(我们选择的解决方案)是防止JBoss在部署时隐式加载Jboss JPA。在这种情况下,您必须在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" />
</exclusions>
</deployment>
</jboss-deployment-structure>

答案 6 :(得分:0)

遇到与JBoss EAP 6.4,JPA 2.1,Spring Boot 1.4.1,Spring Data 1.10.3,Hibernate 5.0.11相同的问题后,这个解决方案对我有用:

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

感谢艾哈迈德的回答,只为org.jboss.logging添加了排除