简体   繁体   English

如何使用 JPA @Query 和 @Param Annotations 执行软删除?

[英]How to perform soft delete using JPA @Query and @Param Annotations?

I am trying to update the existing rows in database table using JPA @Query Annotation.我正在尝试使用 JPA @Query Annotation 更新数据库表中的现有行。 I want to perform Soft delete by updating the Deleted_Flag to YES from NO.我想通过将 Deleted_Flag 从 NO 更新为 YES 来执行软删除。

Here is my Code snippet:这是我的代码片段:

@Modifying
@Query("UPDATE TBL_NAME SET DELETE_FLAG = 'YES' WHERE DELETE_FLAG = 'NO' 
AND FILE_NM = :FILE_NM")
public void softDelete(@Param("FILE_NM") String fileName)
{
}

I am not getting any error, but data is not being updated in database.我没有收到任何错误,但数据库中的数据没有更新。

Actual result must be like all the existing rows must be updated with DELETE_FLAG to YES.实际结果必须像所有现有行都必须使用 DELETE_FLAG 更新为 YES。

也许在使用“更新”查询时使用主键列会与其他“位置”要求一起使用

Make sure you invoke the repository method with an active transaction. 确保使用活动事务调用存储库方法。

Actually, in my last project I use the following idiom for updating a flag : 实际上,在我的上一个项目中,我使用以下惯用法更新标志:

Entity is annotated with Hibernetish: 实体使用Hibernetish进行注释:

@Entity
@Table(name="myTable")
@Where(clause = "is_deleted = 0")
@Cacheable
public class MyTable {}

Actual update comes with a trivial find method: 实际更新附带一个简单的查找方法:

   @Transactional
    public void deleteById(@NonNull final Long themeId) {
        themeRepository.findById(themeId).orElseThrow(() -> new EntityNotFoundException(THEME_NOT_FOUND + themeId))
                .setDeleted(true);
    }

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

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