如何使用Spring Boot JpaRepository及其MySQL脚本保存多个表

时间:2018-11-20 09:28:38

标签: java spring spring-data-jpa

//餐馆实体类

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

/餐厅/ {id}

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

/餐厅/ {id} /菜单/

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脚本

1 个答案:

答案 0 :(得分:-1)

您应该从文档开始: https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html

之后,您可以看到其他示例herehere