【发布时间】:2018-08-24 14:38:01
【问题描述】:
我已经设置了以下 POJO:
@Getter
@Setter
@AllArgsConstructor
@Entity
@Table(name = "SpreadsheetRequest")
public class SpreadsheetRequest {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false, unique = true)
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private Integer id;
private String spreadsheetId;
private String name;
private String range;
@SuppressWarnings("unused")
public SpreadsheetRequest() {
setSpreadsheetId(null);
setName(null);
setRange(null);
}
}
并将我的环境配置如下:
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.initialization-mode=embedded
spring.jpa.properties.hibernate.hbm2ddl.import_files=classpath://resources/import.sql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
import.sql 看起来像这样:
INSERT INTO SPREADSHEET_REQUEST VALUES ('DYsErzRPCLEoYYzQkemEAeerYiQ3Usyn','Test','Sheet1!A1:A12');
我在加载时不断收到以下错误:
Caused by: org.h2.jdbc.JdbcSQLException: Column count does not match; SQL statement:
INSERT INTO SPREADSHEET_REQUEST VALUES ('DYsErzRPCLEoYYzQkemEAeerYiQ3Usyn','Test','Sheet1!A1:A12') [21002-197]
是什么原因造成的?
【问题讨论】:
-
我看到你有这个@Table(name = "SpreadsheetRequest") 那么为什么生成的sql已经插入到电子表格请求中?
-
Spring 默认使用 org.springframework.boot.orm.jpa.SpringNamingStrategy 将驼峰名称转换为带有下划线的大写名称
-
您还应该指定列列表,强制执行您要使用的列及其顺序。它将解耦/保护您免受架构更改(添加列或重新排序,或删除+添加列)
-
添加这个注解@GeneratedValue(strategy = GenerationType.AUTO),你的问题就会消失
-
@MarkRotteveel 试过这个并得到以下错误:
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO SPREADSHEET_REQUEST (SPREADSHEET_ID, NAME, RANGE) VALUES[*]"; expected "("; SQL statement: INSERT INTO SPREADSHEET_REQUEST (SPREADSHEET_ID, NAME, RANGE) VALUES [42001-197]