伙计我正在尝试读取excel文件并在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 :(得分: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>
答案 1 :(得分:0)
看起来像是一个hibernate持久化版本的问题......只需更新你的持久性库..它应该解决错误