java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26616723/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:56:44  来源:igfitidea点击:

java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

javaeclipsehibernatemavenjpa

提问by CodeMed

I am upgrading jpa, hibernate, spring, and jdk to enable the new features from JPA 2.1. But I am getting the following error:

我正在升级 jpa、hibernate、spring 和 jdk 以启用 JPA 2.1 的新功能。但我收到以下错误:

java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;  

I have read other postings about this, but they do not resolve the error on my machine. For instance, SJuan76's answer to this posting suggests upgrading to jpa 2.1, but I have set the eclipse jpa project facet to version 2.1 and I have also included hibernate-jpa-2.1-apiin my pom.xml. You can read all the versions of the various libraries in the complete pom.xml by clicking on this link. What do I need to change in the pom.xml or in the eclipse project to resolve this error?

我已经阅读了关于此的其他帖子,但它们没有解决我机器上的错误。例如,SJuan76 对这篇文章的回答建议升级到 jpa 2.1,但我已将 eclipse jpa 项目方面设置为 2.1 版,并且我也将其包含hibernate-jpa-2.1-api在我的 pom.xml 中。单击此链接,您可以在完整的 pom.xml 中阅读各种库的所有版本。 我需要在 pom.xml 或 eclipse 项目中更改什么才能解决此错误?

Note: I can get rid of the error by changing the hibernate version in the pom.xml back to <hibernate.version>4.2.1.Final</hibernate.version>, but this defeats the purpose of upgrading. Does the fact that changing the hibernate version turns the error on and off help us to diagnose the problem?

注意:我可以通过将 pom.xml 中的 hibernate 版本改回 来消除错误<hibernate.version>4.2.1.Final</hibernate.version>,但这违背了升级的目的。更改休眠版本打开和关闭错误这一事实是否有助于我们诊断问题?

If the problem is multiple jars conflicting with each other, I could use the eclipse type manager to disable one of the jars, but which jar would I look for?

如果问题是多个 jar 相互冲突,我可以使用 eclipse 类型管理器禁用其中一个 jar,但我会寻找哪个 jar?

Here is the complete stack trace:

这是完整的堆栈跟踪:

ERROR ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/business-config.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) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) [catalina.jar:7.0.42]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) [catalina.jar:7.0.42]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_45]
    at java.lang.Thread.run(Thread.java:744) [na:1.7.0_45]
Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
    at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:850) ~[hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:843) ~[hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) ~[hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152) ~[hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
    ... 21 common frames omitted
Oct 28, 2014 11:01:16 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/business-config.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:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    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.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
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:824)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
    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:397)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336)
    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)
... 21 more

采纳答案by Denys

In my case the culprit was javaee-api and glassfish libs that didn't support JPA 2.1

就我而言,罪魁祸首是不支持 JPA 2.1 的 javaee-api 和 glassfish 库

回答by hcura

First you need to figure out which dependency is bringing the wrong JPA. To do this use maven dependency:tree. The output is pretty straightforward and you will be able to determine what to exclude.

首先,您需要弄清楚哪个依赖项带来了错误的 JPA。为此,请使用 maven 依赖项:树。输出非常简单,您将能够确定要排除的内容。

To exclude do something like this:

要排除做这样的事情:

<dependencies>
<dependency>
  <groupId>sample.ProjectA</groupId>
  <artifactId>Project-A</artifactId>
  <version>1.0</version>
  <scope>compile</scope>
  <exclusions>
    <exclusion>  <!-- declare the exclusion here -->
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

Sorry I can't help more but with the information you provide, this is all I can do.

对不起,我无能为力,但根据您提供的信息,这是我所能做的。

Cheers.

干杯。

回答by mrjmh

I resolved the same issue in my project by excluding the Hibernate ejb3 lib that was included via a transitive dependency. The dependency from maven dependency:tree was as follows:

我通过排除通过传递依赖项包含的 Hibernate ejb3 lib 解决了我的项目中的相同问题。来自 maven dependency:tree 的依赖如下:

[INFO] |  +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] |  |  +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile

In my project, this was originally included in the executable jar my build process produced, via the maven assembly plugin. To exclude this (and other libs), I used the assembly plugin's dependencySet configuration in src/main/assemble/test-jar-with-dependencies.xml:

在我的项目中,这最初包含在我的构建过程生成的可执行 jar 中,通过 Maven 程序集插件。为了排除这个(和其他库),我在 src/main/assemble/test-jar-with-dependencies.xml 中使用了程序集插件的 dependencySet 配置:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>test-jar-with-dependencies</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>    
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <excludes>
            <exclude>org.eclipse.persistence</exclude>          
            <exclude>org.hibernate:hibernate-annotations:jar:3.4.0.GA</exclude>
            <exclude>org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA</exclude> 
            <exclude>org.hibernate:ejb3-persistence</exclude>
        </excludes>
        <useProjectArtifact>true</useProjectArtifact>
        <useProjectAttachments>true</useProjectAttachments>
        <unpack>true</unpack>
        <scope>test</scope>
    </dependencySet>
</dependencySets>
</assembly>

Hope that helps!

希望有帮助!

回答by Timmy

you can import hibernate-jpa-2.1-api-1.0.0.Final.jar. it is not ejb3-persistence.jar.

您可以导入 hibernate-jpa-2.1-api-1.0.0.Final.jar。它不是 ejb3-persistence.jar。

回答by sinihong

In case of JBoss7, you should replace the api jar in jboss7/modules/javax/persistence/api with updating module.xml file with the updated jar file.

在 JBoss7 的情况下,您应该使用更新的 jar 文件更新 module.xml 文件替换 jboss7/modules/javax/persistence/api 中的 api jar。

回答by James Akim

If you are using Jboss 7.1.1, jboss already has hibernate-jpa-2.0in its classpath, so during runtime it uses the one in its classpath, not the one you've deployed with your app , to point jboss to the hibernate-jpa-2.1do the following:

如果您使用的是 Jboss 7.1.1,jboss 已经hibernate-jpa-2.0在它的类路径中,所以在运行时它使用它的类路径中的一个,而不是你用你的应用程序部署的那个,指向 jbosshibernate-jpa-2.1执行以下操作:

  1. copy hibernate-jpa-2.1-api-1.0.0.final.jarto the following directory : jboss-as-7.1.1.Final/modules/javax/persistence/api/main

  2. open module.xmland change the resource-root as follows : resource-root path="hibernate-jpa-2.1-api-1.0.0.final.jar"(pointing it to jpa 2.1)

  3. re-compile and re-deploy your app, then you are good to go.

  1. 复制hibernate-jpa-2.1-api-1.0.0.final.jar到以下目录:jboss-as-7.1.1.Final/modules/javax/persistence/api/main

  2. 打开module.xml并更改资源根目录如下:( resource-root path="hibernate-jpa-2.1-api-1.0.0.final.jar"将其指向 jpa 2.1)

  3. 重新编译并重新部署您的应用程序,然后您就可以开始了。

回答by Adam Davis

I spent a lot of time on this.

我花了很多时间在这上面。

For me, it was the javax.persistence.jar conflicting with hibernate jars.

对我来说,是 javax.persistence.jar 与 hibernate jars 冲突。

I removed the persistence.jar and it started working.

我删除了persistence.jar,它开始工作了。