Java Hibernate 4.3.0.Final & Spring Data JPA 1.4.3.RELEASE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21157123/
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
Hibernate 4.3.0.Final & Spring Data JPA 1.4.3.RELEASE
提问by user3201424
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.
我第一次尝试设置和使用 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>
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.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.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.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
我只是无法让这个配置工作。我发现的唯一解决方法是将 Hibernate 恢复到版本 4.2.8.Final
回答by Oliver Drotbohm
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.
您的类路径显然包含 JPA 2.0 和 JPA 2.1 JAR,前者首先被发现,从而导致 Hiberate 失败。从您列出的依赖项声明中,尚不清楚原因,因为 Spring Data JPA 1.4.3 绝对不会将其引入。
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.
因此,我建议尝试mvn dependency:tree
查看 JPA 2.0 JAR 以及谁实际上传递依赖于它(也许您甚至已经在本地声明了它)。如果您仍然坚持使用它,请随时将 Maven 命令的输出添加到您的问题中。
回答by Hrishikesh
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.
如果您确实使用 eclipse,您可以在 eclipse 中打开 pom.xml 并导航到依赖层次结构选项卡,以验证哪个 jar 被选择为传递依赖项,哪个是您配置的那个。您必须选择传递依赖项并删除当前指定的依赖项/或/您必须排除传递依赖项。
回答by Slim BH
<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>
回答by Jewel
I used
我用了
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
编译'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
to sovle it.
解决它。
回答by Ahmad AlMughrabi
If you use Jboss web server, you would like to exclude the hibernate-jpa-2.0-api from pom.xml, for example:
如果您使用 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>
And add hibernate-jpa-2.1-api
dependency to your pom.xml, for example:
并将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>
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.
最后注意:阅读堆栈跟踪,您的查询不是导致错误,而是在EntityManagerFactory
创建时导致的,因为您的某些实体在@Table
注释中使用了此属性。删除属性的使用或将您的库移动到 JPA 2.1。
Also, you need to exclude the jpa subsystem from JBoss, and you would like to do like the following:
此外,您需要从 JBoss 中排除 jpa 子系统,并且您希望执行以下操作:
- Create /WEB-INF/jboss-deployment-structure.xml
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>
- 创建/WEB-INF/jboss-deployment-structure.xml
排除 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>
-Enjoy!
-享受!
回答by Alee
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:
我们遇到了同样的问题。我们有一个使用 Spring 和 Hibernate 4.3.10.FINAL 和 JBoss 7.1.3 的 Web 应用程序。Hibernate 带有 JPA 2.1,JBoss 带有 JPA 2.0。两者在部署应用程序时发生冲突。你有很多解决方案。就像别人说的,你可以把你想要的hibernate-jpa jar的版本放到jboss的modules目录下(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>
回答by Antanas Sinica
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:
在遇到与 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
/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
感谢艾哈迈德的回答,只为 org.jboss.logging 添加了排除