删除 spring 数据中的资源 rest hateoas with relationship
Delete resource in spring data rest hateoas with relationship
我有餐厅的类别、项目和角色。我可以使用邮递员获取和 POST 请求,但是当我尝试删除 "category" 中的条目时,它给出了 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
@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
我也遇到过这个问题。
请尝试为 OneToMany 注释添加以下参数
orphanRemoval = true
我有餐厅的类别、项目和角色。我可以使用邮递员获取和 POST 请求,但是当我尝试删除 "category" 中的条目时,它给出了 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
@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
我也遇到过这个问题。 请尝试为 OneToMany 注释添加以下参数
orphanRemoval = true