线程“main”中的异常 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

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

Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

javahibernate

提问by Md Faisal

The code run successfully if I remove @Tableannotation from the code, and I have checked all the other questions relating this error but could not found any solution.

如果我@Table从代码中删除注释,代码会成功运行,并且我已经检查了与此错误相关的所有其他问题,但找不到任何解决方案。

UserDetails.java

用户详细信息.java

package org.javabrains.faisal.dto;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="USER_DETAILS")
public class UserDetails {

    @Id
    private int userId;

    public Date getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    private String userName;

    private Date joinDate;

    private String address;

    private String description;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

HibernateTest.java

休眠测试.java

package org.javabrain.faisal.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.javabrains.faisal.dto.UserDetails;

public class HibernateTest {

    public static void main(String[] args) {
        UserDetails user=new UserDetails();

        user.setUserId(3);
        user.setUserName("Raza 3");

        user.setAddress("goplganj");
        user.setDescription("from Jamia and TCS");
        user.setJoinDate(new Date());

        SessionFactory sessionFactory= new Configuration().configure().buildSessionFactory();
        Session session =sessionFactory.openSession();

        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();



    }

}

hibernate.cfg.xml

休眠文件.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

        <!-- 3306 is default port number for mysql -->
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- if there is an update then update the database schema on startup(not recreate it) -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class 
        Here we have declared the class which have entities
        -->
        <mapping class="org.javabrains.faisal.dto.UserDetails"/>

    </session-factory>

</hibernate-configuration>

pom.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Error

错误

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
    at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1087)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:767)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProces
sorImpl.java:245)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.processEntityHierarchies(MetadataBuildingProcess.java:222)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.javabrain.faisal.hibernate.HibernateTest.main(HibernateTest.java:22)

回答by v.ladynev

You need to add hibernate-jpa-2.1-api-1.0.0.Final.jarto the classpath.

您需要添加hibernate-jpa-2.1-api-1.0.0.Final.jar到类路径。

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

Table#indexes()method was added in the version 2.1. So you have an incorrect jar (for an example version 2.0) with the @Tableannotation in the class path. It can be in the default libfolder of the application server or the web container.

Table#indexes()方法是在 2.1 版本中添加的。所以你有一个不正确的 jar(例如 2.0 版),@Table在类路径中带有注释。它可以位于lib应用程序服务器或 Web 容器的默认文件夹中。

Solution

解决方案

An incorrect @Tableannotation resides in the persistence-api-1.0.jar. So it is need to delete this jar from the class path. hibernate-jpa-2.1-api-1.0.0.Final.jarhas all annotations which has persistence-api-1.0.jar.

不正确的@Table注释位于persistence-api-1.0.jar. 所以需要从类路径中删除这个jar。hibernate-jpa-2.1-api-1.0.0.Final.jar具有所有具有persistence-api-1.0.jar.

回答by Riadh

Surely you have jpa2.0 jar in your server library. To check for that, you need to unzip all server jars (specially jee jar) and look for Table.class in a package called javax.persistence.

当然,您的服务器库中有 jpa2.0 jar。要检查这一点,您需要解压缩所有服务器 jar(特别是 jee jar)并在名为 javax.persistence 的包中查找 Table.class。