当前位置: 首页 > 知识库问答 >
问题:

mysqlsyntaxerrorexception:您的SQL语法中有一个错误;检查与您的MySQL服务器版本相对应的手册以获得正确的语法

南门焱
2023-03-14

我得到了一个错误。您的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个答案

闻深
2023-03-14

订单是有多个产品,这是好的和正确的也。但从逻辑上讲,把订单和产品联系起来是不对的。因此,我认为,如果只是删除以下线从产品实体,它将解决您的问题。

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