Sesha
Sesha

Reputation: 566

Cannot delete resource in Spring Hateoas

I have a spring boot application which exposes resources using Spring HATEOAS. All the method GET, POST, PATCH works fine except DELETE. When I send a delete request to a resource, it returns 204 No content response but when I request for all resource, the item which I deleted appears again. No exception is logged on the console. No error in postman request.

The resource I am trying to delete is having many-to-one association with another POJO. But those resources which doesn't have many-to-one(some have one-to-many) is getting deleted.

The Mode Entity

@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;
    }


}

The Category Entity

@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;
    }
}

The Expense Entity

@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;
    }

}

The repositories I used

public interface CategoryRepository extends CrudRepository<Category, Integer> {

}

public interface ExpenseRepository extends CrudRepository<Expense, Integer> {

}


public interface ModeRepository extends CrudRepository<Mode, Integer> {

}

The delete request for Expense is not working

I use MySQL as database and use Postman to test the URL

Upvotes: 1

Views: 316

Answers (1)

Shankar Narayanan
Shankar Narayanan

Reputation: 125

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/

Upvotes: 2

Related Questions