简体   繁体   English

Spring 数据 Rest - 软删除

[英]Spring Data Rest - Soft Delete

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 ie 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 .我一直在使用 spring 数据 rest 没有任何问题但现在我有一个要求,当用户对给定实体执行 DELETE 操作时,即DELETE /accounts/<id>我需要在数据库上设置一个标志将该实体标记为已删除,但我确实想保留记录

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.我找不到任何方法来覆盖 delete(ID) 方法的 spring 行为。

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?有任何想法吗?

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 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). (尽管您可以在低级别破解 SpringData,并自动附加此标志)。

  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 )您可以通过将实体侦听器添加到 JPA 定义( 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 delete方法就足够了,如下所示:

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

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.如果实体是该接口的实例,则将删除的标志设置为 true 并调用更新,否则调用超级实现。 Use SimpleJpaRepository instead of JpaRepository.使用 SimpleJpaRepository 而不是 JpaRepository。 Example for interfaces https://github.com/danjee/hibernate-mappings you can find here (Persistent and DefaultPersistent)您可以在此处找到接口https://github.com/danjee/hibernate-mappings 的示例(Persistent 和 DefaultPersistent)

@Autowired
private AccountRepository accountRepository; 
@Override
   public void accountSoftDelete (Long id) {
        Optional<Account> account1= accountRepository.findById(id);
        account1.get().setDeleted(true);
        accountRepository.save(account1.get());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM