2017-06-22 7 views
2

Guys私は、春のアプリケーションでは、Excelファイルを読み込んで情報をデータベースに保存しようとしていますが、私はこのエラーに直面しています。以下は私の仕事です! (私は私のコントローラなどを共有していなかった私は彼らに、その後、私は春上で動作するデータベースを保存する必要があります。Java.langエラー?

メインスプリングアプリケーション

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!"); 

    } 



    } 

私はExcelを読み、春の前にデータを保存しようとしています開始。

私の顧客クラス

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

エラー

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 

ありがとう!

+0

がhttps://stackoverflow.com/questions/20734540/nosuchmethoderror-inを参照してください。エラーを解決する必要があります更新...そのAのような永続性バージョンの問題を休止状態と思われます-javax-persistence-table-indexesljavax-persistence-index – Sudhakar

答えて

0

以下の解決策を試してください。

Hibernate JPAを2.1にアップデートして動作させます。

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

ありがとうございました! – ILLIDAN

関連する問題