MySQLSyntaxErrorException:您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册以获取正确的语法

时间:2018-10-20 08:22:42

标签: mysql spring spring-data-jpa hibernate-mapping

我遇到一个错误。 您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以在第1行的“ order(id)”附近使用正确的语法  不知道为什么。有人可以帮忙吗? 以下是实体。订单可以有很多产品。产品可以与许多订单相关联,而餐厅与产品具有一对一的关系

    @Entity
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToMany
    @JoinTable(name = "order_product", joinColumns = @JoinColumn(name = "order_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "product_id", referencedColumnName = "id"))
    private Set<Product> products = new HashSet<>();

    }


    @Entity
public class Product {

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

    @ManyToOne
    @JoinColumn(name = "restaurant_id", referencedColumnName = "id",  insertable=false, updatable=false)
    @JsonBackReference
    private Restaurant restaurant;
    private int restaurant_id;
    private String name;
    private String description;
    private float price;
    private float discount;

    @ManyToMany(fetch = FetchType.EAGER)
    @JsonIgnore
    private Set<Order> orders = new HashSet<>();
    }



 @Entity
public class Restaurant {
    @Id
    @GeneratedValue
    private int id;
    @NotNull
    private String name;
    private String city;
    private String address;
    private String email;
    private String phone;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "restaurant", cascade = CascadeType.ALL)
    @JsonManagedReference
    private List<Product> menu;
}

1 个答案:

答案 0 :(得分:0)

订单中有多个产品还可以,而且也正确。但是从逻辑上讲,将订单与产品关联是不正确的。因此,我认为,只要从“产品”实体中删除以下行即可解决您的问题。

@ManyToMany
@JoinTable(name = "order_product", joinColumns = @JoinColumn(name = "product_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name ="order_id", referencedColumnName = "order_id"))
private Set<Order> order;