执行 DDL 时出错“在 Spring 数据 JPA 中创建 table?

Error executing DDL "create table in Spring Data JPA?

我想保存一些来自请求的数据。为此,我创建了一些实体 classes。但为什么我遇到这个例外?

Error executing DDL "create table body_datum (body_datum_id integer not null, key varchar(255), value varchar(255), value_type varchar(255), primary key (body_datum_id)) engine=InnoDB" via 

这是我的属性。

spring.datasource.url= jdbc:mysql://localhost:3306/loadtestdb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create

这是我的模型 classes.

@Data
@Entity
public class BodyDatum {

    @Id
    @GeneratedValue
    private Integer bodyDatumId;

    private String key;

    private String value;

    private String valueType;
}

产品class喜欢这样

@Data
@Entity
public class Product {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long pId;
    private String productName;
    private int pQty;
    private int price;

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
}

在数据库中,产品 table 已创建。

为什么会出现这样的错误?

谢谢。

我认为属性 "key" 是 SQL 的保留字。像这样重命名您的属性:

@Data
@Entity
public class BodyDatum {

    @Id
    @GeneratedValue
    private Integer bodyDatumId;

    private String bodyDatumkey;

    private String value;

    private String valueType;
}

我在同一条船上。

除了@RUARO提到的MySQL保留字问题外,我还犯了另一个错误,即使我用非保留字替换列名后问题仍然存在:

application.properties中的spring.datasource.url字段中,我应该在连接结束时指定数据库名称url,即

jdbc:mysql://localhost:3306/mydatabasename

而不是

jdbc:mysql://localhost:3306

配置 application.properties 文件。

project > src > main > resources > application.properties

根据您的 Java 版本配置 Hibernate 方言。

对于 MySql 5 使用 MySQL5Dialect 或对于 MySql 8 使用 MySQL8Dialect

像这样:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect