11

I've been using spring data rest without any problem but now I have a requirement that when a user performs a DELETE operation on a given entity i.e. DELETE /accounts/<id> I need to set a flag on the database marking that entity as deleted but i do want to keep the record.

Basically this means that I need to do an UPDATE instead of a DELETE operation in the database. I don't find any way to override the spring behavior for the delete(ID) method.

Some code:

@Entity
@Table(name = "account")
public class Account {

    /*
Default value for this field is false but when a receive a 
DELETE request for this entity i want to turn this flag 
to false instead of deleting the record.
    */
    @Column(name = "deleted")
    private boolean deleted;

...
}

Account Repository

@RepositoryRestResource
public interface AccountRepository extends JpaRepository<Account, Integer> {

}

Any ideas?

ejoncas
  • 329
  • 2
  • 13
  • 2
    See my answer: [handling-soft-deletes-with-spring-jpa](http://stackoverflow.com/questions/19323557/handling-soft-deletes-with-spring-jpa/33168644#33168644) – 易天明 Oct 24 '16 at 08:44

4 Answers4

6

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)

Community
  • 1
  • 1
mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • 1
    Thanks I was able to do this using an annotation @SoftDelete in the entity creating and following that spring tutorial. (http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#repositories.custom-implementations) – ejoncas Sep 23 '15 at 06:53
  • 2
    hello ejoncas, could you elaborate a bit on how you were able to setup that annotation and use it? – gtiwari333 Feb 08 '16 at 16:33
  • 2
    I don't think @SoftDelete annotation has been released yet by spring-data-common – prakashpoudel Mar 17 '16 at 23:51
4

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);
}
Tomek Samcik
  • 526
  • 4
  • 7
0

I think first you should use an interface to identify only the entities that will use the soft delete. Afterwards you can override the delete method. If the entity is instance of that interface set the deleted flag to true and call update else call the super implementation. Use SimpleJpaRepository instead of JpaRepository. Example for interfaces https://github.com/danjee/hibernate-mappings you can find here (Persistent and DefaultPersistent)

Daniel Jipa
  • 878
  • 11
  • 24
0
@Autowired
private AccountRepository accountRepository; 
@Override
   public void accountSoftDelete (Long id) {
        Optional<Account> account1= accountRepository.findById(id);
        account1.get().setDeleted(true);
        accountRepository.save(account1.get());
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 02 '22 at 06:29