2

Guys I am trying to read an excel file and save informations to database in a spring application but i am facing with this error. Here's my work below! (I did not share my controller etc. I just need to save them database after that I will work on spring.

Main Spring Application

package com.javainuse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootHelloWorldApplication {

    public static void main(String[] args) throws IOException {         
                System.out.println("Debug Print");
                 Read();


        SpringApplication.run(SpringBootHelloWorldApplication.class, args);

    }

    private static void Read() throws IOException {
          System.out.println("Debug print");
        SessionFactory sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
        Session session = sf.openSession();
        FileInputStream file = new FileInputStream(new File("C:/Users/MONSTER/Desktop/Hello.xlsx")); 
        XSSFWorkbook workbook = new XSSFWorkbook(file); 
        XSSFSheet sheet = workbook.getSheetAt(0); 
        Row row;
        for(int i=1; i<=sheet.getLastRowNum(); i++){ 
            row = (Row) sheet.getRow(i);  //sheet number


                String id;
                if( row.getCell(0)==null) { id = "0"; }
                else id= row.getCell(0).toString();

                   String name;
                if( row.getCell(1)==null) { name = "null";}  
                   else name = row.getCell(1).toString();   //else copies cell data to name variable

                   String phone;
                if( row.getCell(2)==null) { phone = "null";   }
                   else  phone   = row.getCell(2).toString();
        Transaction t = session.beginTransaction();
        Customer std = new Customer();
        std.setId((int) Double.parseDouble(id));
        std.setName(name);
        std.setPhone(phone);
        System.out.println(std.getId()+" "+std.getName()+" "+std.getPhone());
        session.saveOrUpdate(std);
        t.commit();     
        }

        file.close();
                System.out.println("Completed!");

    }



    }

I am trying to read excel and save data before the spring starts.

My customer class

package com.javainuse;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer", schema="excel")
public class Customer  {
    @Id
 //  @GeneratedValue(strategy = GenerationType.AUTO)
private int id;
        @Column
private String name;
        @Column
private String phone;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public Customer(int id, String name, String phone) {
    super();
    this.id = id;
    this.name = name;
    this.phone = phone;
}
public Customer() {
    super();
}





}

Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/excel</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <mapping class="com.javainuse.Customer"/>
    </session-factory>
</hibernate-configuration>

Error

Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
    at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:973)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
    at com.javainuse.SpringBootHelloWorldApplication.Read(SpringBootHelloWorldApplication.java:31)
    at com.javainuse.SpringBootHelloWorldApplication.main(SpringBootHelloWorldApplication.java:22)
14:05:19.071 [pool-1-thread-1] DEBUG org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - Connection pool now considered primed; min-size will be maintained

Thanks!

ILLIDAN
  • 29
  • 6
  • See the https://stackoverflow.com/questions/20734540/nosuchmethoderror-in-javax-persistence-table-indexesljavax-persistence-index – Sudhakar Jun 22 '17 at 11:16

2 Answers2

0

Try the below solution.

Update Hibernate JPA to 2.1 and It works.

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>
Sudhakar
  • 3,104
  • 2
  • 27
  • 36
0

seems like its a hibernate persistence version problem ...just update your persistence library..it should solve the error

rackdevelop247
  • 86
  • 1
  • 10