NoSuchMethodError in javax.persistence.Table.indexes()[Ljavax/persistence/Index

前端 未结 9 1125
面向向阳花
面向向阳花 2020-11-22 14:40

I have a Play Framework application and I was using Hibernate 4.2.5.Final (which is retrieved via the Maven dependency manager). I decided to upgrade to Hibernate

相关标签:
9条回答
  • 2020-11-22 14:46

    There are multiple JPA providers in your classpath. Or atleast in your Application server lib folder.

    If you are using Maven Check for dependencies using command mentioned here https://stackoverflow.com/a/47474708/3333878

    Then fix by removing/excluding unwanted dependency.

    If you just have one dependecy in your classpath, then the application server's class loader might be the issue.

    As JavaEE application servers like Websphere, Wildfly, Tomee etc., have their own implementations of JPA and other EE Standards, The Class loader might load it's own implementation instead of picking from your classpath in WAR/EAR file.

    To avoid this, you can try below steps.

    1. Removing the offending jar in Application Servers library path. Proceed with Caution, as it might break other hosted applications.

    In Tomee 1.7.5 Plume/ Web it will have bundled eclipselink-2.4.2 in the lib folder using JPA 2.0, but I had to use JPA 2.1 from org.hibernate:hibernate-core:5.1.17, so removed the eclipselink jar and added all related/ transitive dependencies from hibernate core.

    1. Add a shared library. and manually add jars to the app server's path. Websphere has this option.

    2. In Websphere, execution of class loader can be changed. so making it the application server's classpath to load last i.e, parent last and having your path load first. Can solve this.

    Check if your appserver has above features, before proceeding with first point.

    Ibm websphere References :

    https://www.ibm.com/support/knowledgecenter/SSEQTP_9.0.5/com.ibm.websphere.base.doc/ae/trun_classload_server.html

    https://www.ibm.com/support/pages/how-create-shared-library-and-associate-it-application-server-or-enterprise-application-websphere-application-server

    0 讨论(0)
  • 2020-11-22 14:50

    I had the same issue, I fixed it by using org.hibernate.annotations.Table annotation instead of javax.persistence.Table in the Entity class.

    import javax.persistence.Entity;
    import org.hibernate.annotations.Table;
    
    @Entity
    @Table(appliesTo = "my_table")
    public class MyTable{
    //and rest of the code
    
    0 讨论(0)
  • 2020-11-22 14:52

    I've ran into the same problem. The question here is that play-java-jpa artifact (javaJpa key in the build.sbt file) depends on a different version of the spec (version 2.0 -> "org.hibernate.javax.persistence" % "hibernate-jpa-2.0-api" % "1.0.1.Final").

    When you added hibernate-entitymanager 4.3 this brought the newer spec (2.1) and a different factory provider for the entitymanager. Basically you ended up having both jars in the classpath as transitive dependencies.

    Edit your build.sbt file like this and it will temporarily fix you problem until play releases a new version of the jpa plugin for the newer api dependency.

    libraryDependencies ++= Seq(
    javaJdbc,
    javaJpa.exclude("org.hibernate.javax.persistence", "hibernate-jpa-2.0-api"),
    "org.hibernate" % "hibernate-entitymanager" % "4.3.0.Final"
    )
    

    This is for play 2.2.x. In previous versions there were some differences in the build files.

    0 讨论(0)
  • 2020-11-22 14:52

    Hibernate 4.3 is the first version to implement the JPA 2.1 spec (part of Java EE 7). And it's thus expecting the JPA 2.1 library in the classpath, not the JPA 2.0 library. That's why you get this exception: Table.indexes() is a new attribute of Table, introduced in JPA 2.1

    0 讨论(0)
  • 2020-11-22 14:52

    You probablly have 2 different versions of hibernate-jpa-api on the classpath. To check that run:

    mvn dependency:tree >dep.txt
    

    Then search if there are hibernate-jpa-2.0-api and hibernate-jpa-2.1-api. And exclude the excess one.

    0 讨论(0)
  • 2020-11-22 14:56

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

    The only thing that solved my problem was removing the following dependency in pom.xml: <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency>

    And replace it for:

    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0.2</version>
    </dependency>
    

    Hope it helps someone.

    0 讨论(0)
提交回复
热议问题