【问题标题】:How to save multiple tables using Spring Boot JpaRepository and their MySQL script如何使用 Spring Boot JpaRepository 及其 MySQL 脚本保存多个表
【发布时间】:2018-11-20 09:28:38
【问题描述】:

//餐厅实体类

public class Restaurant {

    @GeneratedValue
    @Id
    private Long id;

    @NotBlank
    @Size(min = 3, max = 50)
    private String name;

    @OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private List<Menu> menus;
}

//菜单实体类

public class Menu {

    @Id
    private Long id;

    @NotBlank
    @Size(min = 3, max = 50)
    private String type;

    @NotBlank
    private String info;

    @ManyToOne
    @JoinColumn(name = "restaurant_id")
    private Restaurant restaurant;
}

我有两个实体:一个是餐厅,另一个是带有端点的菜单

/餐厅

GET - get all restaurants
POST - upload restaurant
PUT - Update restaurant
DELETE - delete all restaurant

/restaurants/{id}

GET - get a restaurant by id
DELETE - delete a restaurant by id

/restaurants/{id}/menus/

GET - get all menus in the restaurant specified by id
POST - add new menus in the restaurant specified by id
DELETE - delete all menus in the restaurant specified by id

现在请您检查一下实体类是否正确,并请向我提供上述实体的 MySQL 脚本

【问题讨论】:

    标签: java spring spring-data-jpa


    【解决方案1】:

    【讨论】:

    • 能否提供上面的sql脚本
    • 请阅读文档。如果使用 Spring Data Jpa Repository,则无需编写 sql 脚本。框架会为你做这件事
    猜你喜欢
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2020-11-10
    • 2019-05-15
    相关资源
    最近更新 更多