Spring Data Rest - Soft Delete

Try to create a custom repository, to see how it would play out

http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#repositories.custom-implementations

But delete is not the only place you'll need to change your logic. I see 2 ways to handle the flag requirement:

  1. Have an extra flag in your entity definition, and update it on Delete.

    In this case you need to be careful, and rewrite all existing queries, to be sure, that removed entities would not be returned, and keep in mind this separation of results, for all future entities. (Although you can hack SpringData on low level, and append this flag automatically).

  2. Delete entity from original collection and add it to another collection, where entities are stored before complete disposal.

    In this case you'll need to have additional logic for managing disposal collections, but this has no implications on query logic. You can integrate with your existing application, by adding entity listener to your JPA definition (http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#jpa.auditing)


It's enough that you override delete method of your @RepositoryRestResource, like so:

@RepositoryRestResource
public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {

    @Modifying
    @Query("update Product p set deleted = true where p = :p")
    void delete(Product p);

    @Query("select p FROM Product p WHERE p.deleted = false")
    Page<Product> findAll(Pageable pageable);
}