由以下原因引起:java.sql.SQLSyntaxErrorException:[...]版本,用于在第1行的'order(id)'附近使用正确的语法

时间:2019-08-15 08:03:55

标签: mysql hibernate spring-boot spring-data-jpa many-to-one

此异常与

有关
spring.jpa.hibernate.ddl-auto=update

我还没有尝试保存任何数据

我认为映射是正确的,但仍然抛出异常:

org.hibernate.tool.schema.spi.CommandAcceptanceException:通过JDBC语句执行DDL“创建表顺序(id bigint不为null的auto_increment,order_date日期,user_id bigint,主键(id))engine = MyISAM)时出错

由以下原因引起:java.sql.SQLSyntaxErrorException:您的SQL语法有错误。检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ order(id)”附近使用

我的映射如下:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseEntity {

    @Id
    @GeneratedValue(
            strategy = GenerationType.IDENTITY
    )
    private Long id;
}


@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "Order")
@Table(name = "order")
public class Order extends BaseEntity {

    private LocalDate orderDate;

    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    private List<OrderedTicket> orderedTickets = new ArrayList<>();

    private Long userId;

}


@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "OrderedTicket")
@Table(name = "ordered_ticket")
public class OrderedTicket extends BaseEntity {

    private Integer amount;

    @OneToOne
    @JoinColumn(name="ticket_id")
    private Ticket ticket;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "order_id")
    private Order order;

}


@Getter
@Setter
@NoArgsConstructor
@Entity(name = "Ticket")
@Table(name = "ticket")
public class Ticket extends BaseEntity {

    @Enumerated(value = EnumType.STRING)
    private TicketType type;
    private Integer price;
    private Integer totalAmmount;
    private Integer inStock;

    @ManyToOne(cascade = {
            CascadeType.DETACH,
            CascadeType.MERGE,
            CascadeType.PERSIST,
            CascadeType.REFRESH
    })
    @JoinColumn(name = "event_id")
    Event event;

    @Builder
    public Ticket(TicketType type, Integer price, Integer totalAmmount, Integer inStock, Event event) {
        this.type = type;
        this.price = price;
        this.totalAmmount = totalAmmount;
        this.inStock = inStock;
        this.event = event;
    }

}

3 个答案:

答案 0 :(得分:1)

ORDER是保留字(用于ORDER BY子句) 您应该更改表名,例如使用其他表名

create table customer_order (
  id bigint not null auto_increment
, order_date date
, user_id bigint
, primary key (id)) 
engine=MyISAM" 

或者如果您确实需要此表名,请在表名前后使用反斜线

create table `order` (
  id bigint not null auto_increment
, order_date date
, user_id bigint
, primary key (id)) 
engine=MyISAM" 

答案 1 :(得分:1)

尝试用反引号括住订单表名称。否则,引擎会由于保留关键字而不断抛出错误:

@Table(name = "`order`")
public class Order extends BaseEntity {

答案 2 :(得分:0)

您不应该为表ORDER命名,因为ORDER是保留的SQL关键字。

您可以执行此操作,但随后始终必须将ORDER括在引号中。

请查看MySQL手册中所有保留的关键字:

https://dev.mysql.com/doc/refman/8.0/en/keywords.html