Spring Data JPA OneToMany Relationship奇怪的错误

时间:2019-01-20 17:55:16

标签: spring hibernate spring-data

我有2个看起来像这样的实体:

@Entity(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String username;

    @OneToMany(mappedBy = "user")
    private List<Option> options = new ArrayList<>();
    getters and setters...

@Entity
public class Option {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private int option;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
    setters and getters

此外,我在application.properties中具有这些属性:

spring.datasource.url=jdbc:mysql://localhost:3306/dbtest
spring.datasource.username=something
spring.datasource.password=something
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

每次我运行应用程序时,它都会给我以下信息:

Error executing DDL "create table option (id integer not null, option integer not null, user_id integer, primary key (id)) engine=MyISAM" via JDBC Statement

我不明白我在做什么错:(

2 个答案:

答案 0 :(得分:1)

option是MySql中的保留字。尝试为属性和表使用其他名称。

答案 1 :(得分:1)

不要使用名称为Option和user的表,因为它是SQL中的语句。