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

j2datikz  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(335)

我犯了个错误。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;
}
mfpqipee

mfpqipee1#

订单是有多个产品是好的和正确的。但从逻辑上讲,把订单和产品联系起来是不对的。所以我认为,只要从产品实体中删除以下行,就可以解决您的问题。

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

相关问题