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

时间:2016-02-23 04:26:56

标签: java hibernate

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

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)

2 个答案:

答案 0 :(得分:8)

您需要将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>
版本2.1中添加了

Table#indexes()方法。因此,类路径中的@Table注释具有不正确的jar(对于示例版本2.0)。它可以位于应用程序服务器的默认lib文件夹或Web容器中。

<强>解决方案

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

答案 1 :(得分:0)

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