在 Spring 引导应用程序中出现 MySQL 语法错误
Getting MySQL syntax error in Spring Boot Application
我正在制作一个简单的应用程序,使用 Spring Boot with Gradle 并尝试将其连接到我计算机上的 MySQL。当我 运行 应用程序时,出现以下错误:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'load (id integer not null, customer varchar(255), date datetime not null, destin' at line 1
现在我只有一个 class,加载,这是它的样子:
@Entity
public class Load {
public Load(){}
@Id
@GeneratedValue
private int id;
@NotBlank(message = "Customer field must not be blank")
@NotNull
@Size(max = 100, message = "Customer name too long!")
private String customer;
@NotBlank(message = "Destination field must not be blank")
@NotNull
@Size(max = 100, message = "Destination name too long!")
private String destination;
@NotBlank(message = "Driver field must not be blank")
@NotNull
@Size(max = 50, message = "Driver name too long!")
private String driver;
@NotNull
private Date date;
public Load(String customer, String destination, String driver, Date date) {
this.customer = customer;
this.destination = destination;
this.driver = driver;
this.date = date;
}
我不太确定我在这个 class
中创建的问题出在哪里
LOAD
是 MySQL 中的 reserved keyword,这就是错误消息指向 SQL 语句中名称 load
的原因("near 'load").
通过使用 @Entity
注释的 name
属性指定值名称来重命名 table,例如
@Entity(name = "customer_load")
public class Load {
您正在尝试使用 MySQL 保留关键字 Load
作为 table 名称,这就是问题所在。
但是您也可以在使用反引号时使用保留关键字。
@Entity
@javax.persistence.Table(name = "`load`")
public class Load{
...
}
我正在制作一个简单的应用程序,使用 Spring Boot with Gradle 并尝试将其连接到我计算机上的 MySQL。当我 运行 应用程序时,出现以下错误:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'load (id integer not null, customer varchar(255), date datetime not null, destin' at line 1
现在我只有一个 class,加载,这是它的样子:
@Entity
public class Load {
public Load(){}
@Id
@GeneratedValue
private int id;
@NotBlank(message = "Customer field must not be blank")
@NotNull
@Size(max = 100, message = "Customer name too long!")
private String customer;
@NotBlank(message = "Destination field must not be blank")
@NotNull
@Size(max = 100, message = "Destination name too long!")
private String destination;
@NotBlank(message = "Driver field must not be blank")
@NotNull
@Size(max = 50, message = "Driver name too long!")
private String driver;
@NotNull
private Date date;
public Load(String customer, String destination, String driver, Date date) {
this.customer = customer;
this.destination = destination;
this.driver = driver;
this.date = date;
}
我不太确定我在这个 class
中创建的问题出在哪里LOAD
是 MySQL 中的 reserved keyword,这就是错误消息指向 SQL 语句中名称 load
的原因("near 'load").
通过使用 @Entity
注释的 name
属性指定值名称来重命名 table,例如
@Entity(name = "customer_load")
public class Load {
您正在尝试使用 MySQL 保留关键字 Load
作为 table 名称,这就是问题所在。
但是您也可以在使用反引号时使用保留关键字。
@Entity
@javax.persistence.Table(name = "`load`")
public class Load{
...
}