2017-06-22 18 views
2

伙计们我试图读取一个excel文件并将信息保存到数据库中的一个spring应用程序,但我面临着这个错误。以下是我的工作! (我不同意我的控制器等等。我只是需要将其保存的数据库后,我会在春天工作。Java.lang错误?

主要Spring应用程序

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

0

好像它是一个Hibernate持久的版本问题...只是更新你的持久library..it应该解决错误

相关问题