无法删除Spring Hateoas中的资源

时间:2018-01-16 19:20:39

标签: java mysql spring-boot spring-data-jpa spring-hateoas

我有一个Spring Boot应用程序,它使用Spring HATEOAS公开资源。除DELETE之外,所有方法GET,POST,PATCH都可以正常工作。当我向资源发送删除请求时,它返回204无内容响应,但是当我请求所有资源时,我删除的项目再次出现。控制台上没有记录异常。邮递员请求中没有错误。

我想删除的资源与另一个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 个答案:

答案 0 :(得分:1)

Try Changing from the cascade cascade = CascadeType.ALL)

and set cascade = CascadeType.REMOVE, orphanRemoval = true it should work

Read the docs for more information: https://docs.oracle.com/cd/E19798-01/821-1841/giqxy/