线程"main"中的异常java.lang.NoSuchMethodError:javax.persistence.Table.indexes()[Ljavax/persistence/Index; [英] Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

查看:53
本文介绍了线程"main"中的异常java.lang.NoSuchMethodError:javax.persistence.Table.indexes()[Ljavax/persistence/Index;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从代码中删除了@Table批注,则代码成功运行,并且我检查了与该错误有关的所有其他问题,但找不到任何解决方案.

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

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

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

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

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

错误

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$1.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)

推荐答案

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

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

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

Table#indexes()方法已在版本2.1中添加.因此,您在类路径中使用了@Table批注的jar(例如2.0版示例)不正确.它可以位于应用程序服务器或Web容器的默认lib文件夹中.

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

解决方案

persistence-api-1.0.jar中存在错误的@Table批注.因此,需要从类路径中删除此jar. hibernate-jpa-2.1-api-1.0.0.Final.jar具有所有带有persistence-api-1.0.jar的注释.

An incorrect @Table annotation 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.jar has all annotations which has persistence-api-1.0.jar.

这篇关于线程"main"中的异常java.lang.NoSuchMethodError:javax.persistence.Table.indexes()[Ljavax/persistence/Index;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆