我有一个springboot应用程序,它使用springhateoas公开资源。所有的方法get,post,patch都可以正常工作,除了delete。当我向资源发送删除请求时,它不会返回任何内容响应,但当我请求所有资源时,我删除的项目会再次出现。控制台上没有记录异常。 Postman 请求中没有错误。
我试图删除的资源与另一个pojo有多对一的关联。但是那些没有多对一(有些是一对多)的资源正在被删除。
模式实体
@Entity
@Table(name="Modes")
public class Mode {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy = "mode", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Expense> expense;
public Mode() {}
@Autowired
public Mode(String name,Set<Expense> expense) {
this.name = name;
this.expense = expense;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
类别实体
@Entity
@Table(name="Categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy = "category", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Expense> expense;
public Category() { }
@Autowired
public Category(String name, Set<Expense> expense) {
this.setName(name);
this.expense = expense;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
费用主体
@Entity
@Table(name="Expenses")
public class Expense {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private BigDecimal amount;
@ManyToOne
@JoinColumn(name="categoryId")
private Category category;
@ManyToOne
@JoinColumn(name="modeId")
private Mode mode;
private Date date;
public Expense() {}
public Expense(String name, BigDecimal amount, Category category, Mode mode, Date date) {
this.name = name;
this.amount = amount;
this.category = category;
this.mode = mode;
this.date = date;
}
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 BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Mode getMode() {
return mode;
}
public void setMode(Mode mode) {
this.mode = mode;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
我使用的存储库
public interface CategoryRepository extends CrudRepository<Category, Integer> {
}
public interface ExpenseRepository extends CrudRepository<Expense, Integer> {
}
public interface ModeRepository extends CrudRepository<Mode, Integer> {
}
费用删除请求无效
我使用mysql作为数据库,并使用postman测试url
1条答案
按热度按时间h7appiyu1#
尝试从cascade=cascadetype.all更改
设置cascade=cascadetype.remove,orphanRemoving=true应该可以
阅读文档了解更多信息:https://docs.oracle.com/cd/e19798-01/821-1841/giqxy/