删除春天数据中的资源休息hateoas与关系

时间:2018-01-14 11:49:24

标签: spring spring-boot one-to-many spring-data-rest hateoas

我有一个餐厅的类别,项目和角色。我可以使用邮递员获取和POST请求,但是当我尝试删除"类别"它给出了204 - 没有内容,但数据仍未被删除。这是因为该类别与餐厅有多对一的关系。另一方面,如果我对角色发出DELETE请求,那么它完美地起作用并没有任何直接的关系。

类别的POJO

@Entity
@Table(name="Categories")
public class Category { 

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

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

    @OneToMany(mappedBy = "category", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Item> items;

    public Category () {
    }

    @Autowired
    public Category(String name, Restaurant restaurant, Set<Item> items) {      
        this.name = name;
        this.restaurant = restaurant;
        this.items = items;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Set<Item> getItems() {
        return items;
    }
    public void setItems(Set<Item> items) {
        this.items = items;
    }
    public Restaurant getRestaurant() {
        return restaurant;
    }
    public void setRestaurant(Restaurant restaurant) {
        this.restaurant = restaurant;
    }
}

POJO for Roles

@Entity
@Table(name="Roles")
public class Role {

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

    @OneToMany(mappedBy = "role", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<User> users;

    public Role() {
    }   

    @Autowired
    public Role(String name, Set<User> users) {     
        this.name = name;
        this.users = users;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Set<User> getUser() {
        return users;
    }
    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

类别信息库

public interface CategoryRepository extends CrudRepository<Category, Integer> {

}

删除邮递员的请求

DELETE localhost:9090/api/categories/7
Content-type: text/uri-list 
Response: 204 no content

1 个答案:

答案 0 :(得分:0)

我也遇到过这个问题。 请尝试为OneToMany注释添加以下参数

orphanRemoval = true