在使用 h2 数据库和 JPA 运行 spring boot 时,我遇到了以下错误。
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:440) [hibernate-core-5.2.17.Final.jar:5.2.17.Final]
这是由于以下之一引起的
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE EXCHANGE_VALUE (ID INTEGER NOT NULL, CONVERSION_MULTIPLE DECIMAL(19,2), FROM[*] VARCHAR(255), PORT INTEGER NOT NULL, TO VARCHAR(255), PRIMARY KEY (ID)) "; expected "identifier"; SQL statement:
create table exchange_value (id integer not null, conversion_multiple decimal(19,2), from varchar(255), port integer not null, to varchar(255), primary key (id)) [42001-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357) ~[h2-1.4.197.jar:1.4.197]
at org.h2.message.DbException.getSyntaxError(DbException.java:217) ~[h2-1.4.197.jar:1.4.197]
我的冬眠课
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Exchange_Value")
public class ExchangeValue {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String from;
private String to;
private BigDecimal conversionMultiple;
private int port;
public ExchangeValue() {
}
public ExchangeValue(String from, String to, BigDecimal conversionMultiple) {
super();
// this.id = id;
this.from = from;
this.to = to;
this.conversionMultiple = conversionMultiple;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
application.properties 如下
spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.hibernate.ddl-auto= create-drop
只是想知道我在代码中缺少什么尝试添加 spring.jpa.hibernate.ddl-auto= create-drop 但它没有帮助。
原文由 shubh 发布,翻译遵循 CC BY-SA 4.0 许可协议
@shubh .. 您的实体字段名称与
SQL reserved keywords
匹配,因此,请尝试更改字段名称,否则请使用
name
属性与@Column Annotation
(它为数据库提供别名)