2

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>

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$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)
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
Md Faisal
  • 2,931
  • 8
  • 29
  • 34

2 Answers2

9

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() 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.

Solution

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.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • I checked my libraries for the same but could not find the incorrect version. Also if that's the case then how can I get rid of it. – Md Faisal Feb 23 '16 at 10:41
  • @MdFaisal Is it possible to add above `HibernateTest` `@Table(indexes = {})` without errors? – v.ladynev Feb 23 '16 at 11:27
  • No, It says :-"The attribute indexes is undefined for the annotation type Table" – Md Faisal Feb 23 '16 at 12:46
  • @MdFaisal So you have an incorrect jar in the class path :) – v.ladynev Feb 23 '16 at 12:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104295/discussion-between-md-faisal-and-v-ladynev). – Md Faisal Feb 23 '16 at 13:16
  • This worked for me... thanx , however inside hibernate-jpa-2.1-api jar still it uses javax.persistence . Therefore we have to remove it manuly form reference libraries in IDE, after removing from pom and build... Thanx again... – Kandy May 01 '17 at 17:22
0

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.

Riadh
  • 1,088
  • 2
  • 12
  • 25