实体关系映射

建了四个实体,启动程序。

clipboard.png

打开数据库,发现只生成了三张表。

clipboard.png

OrderDetailProductCategoryProductInfo都创建成功,只有Order表创建失败。

同时,控制台也报出了如下错误:

org.hibernate.tool.hbm2ddl.SchemaExport  : 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 'order' at line 1
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: create table order (id varchar(255) not null, amount decimal(19,2), buyer_address varchar(255), buyer_name varchar(255), buyer_openid varchar(255), buyer_phone varchar(255), create_time datetime, pay_status integer, status integer, update_time datetime, primary key (id))
org.hibernate.tool.hbm2ddl.SchemaExport  : 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 'order (id varchar(255) not null, amount decimal(19,2), buyer_address varchar(255' at line 1
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table order_detail add constraint FKplam7wxc4tjbgex0xyk8f0qxo foreign key (order_id) references order (id)
org.hibernate.tool.hbm2ddl.SchemaExport  : 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 'order (id)' at line 1

大致意思就是你的Order表中有一个SQL语法错误,导致了创建order表失败。

Order实体

语法错误,第一反应就是去看看Order实体,应该是哪里写错了。

package com.imooc.spring.entity;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;
import java.util.Calendar;

/**
 * @author zhangxishuo on 2018/4/25
 * 订单主表
 */

@Entity
public class Order {

    public static final Integer NEW = 0;         // 新下单

    public static final Integer FINISH = 1;      // 已完结

    public static final Integer CANCEL = 2;      // 已取消

    public static final Integer WAIT =  0;       // 未支付

    public static final Integer SUCCESS = 1;     // 支付成功

    @Id
    private String id;                    // 订单id

    private String buyerName;             // 买家姓名

    private String buyerPhone;            // 买家电话

    private String buyerAddress;          // 买家地址

    private String buyerOpenid;           // 买家Openid

    private BigDecimal amount;            // 总金额

    private Integer status = NEW;         // 订单状态

    private Integer payStatus = WAIT;     // 支付状态

    @CreationTimestamp
    private Calendar createTime;          // 创建时间

    @UpdateTimestamp
    private Calendar updateTime;          // 更新时间

    public Order() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBuyerName() {
        return buyerName;
    }

    public void setBuyerName(String buyerName) {
        this.buyerName = buyerName;
    }

    public String getBuyerPhone() {
        return buyerPhone;
    }

    public void setBuyerPhone(String buyerPhone) {
        this.buyerPhone = buyerPhone;
    }

    public String getBuyerAddress() {
        return buyerAddress;
    }

    public void setBuyerAddress(String buyerAddress) {
        this.buyerAddress = buyerAddress;
    }

    public String getBuyerOpenid() {
        return buyerOpenid;
    }

    public void setBuyerOpenid(String buyerOpenid) {
        this.buyerOpenid = buyerOpenid;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getPayStatus() {
        return payStatus;
    }

    public void setPayStatus(Integer payStatus) {
        this.payStatus = payStatus;
    }

    public Calendar getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Calendar createTime) {
        this.createTime = createTime;
    }

    public Calendar getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Calendar updateTime) {
        this.updateTime = updateTime;
    }
}

解决方案

看看这个实体,并没有找到有什么错误。

然后就去对比慕课网的代码,发现讲师建的订单实体名为OrderMaster,我这里的代码与讲师的代码除了实体名不同其他完全一样。

试着将实体名从Order修改为OrderMaster

clipboard.png

改完名,妥妥地成功。

原因

猜测应该是Order属于SQL中的关键字,所以会报语法错误。

把语法错误的报错信息加上orderGoogle一下。

Unsuccessful: create table order You have an error in your SQL syntax; order

clipboard.png

果然,StackOverflow给出了一个非常完美的解释。

Order实体会默认映射到order表,但是ordersql中的一个服务关键字。所以生成order表的时候异常就发生了。


张喜硕
2.1k 声望423 粉丝

浅梦辄止,书墨未浓。