如何使用条件(where子句)更新实体并在spring数据jpa中的方法响应中获取更新的实体

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用条件(where子句)更新实体并在spring数据jpa中的方法响应中获取更新的实体相关的知识,希望对你有一定的参考价值。

我正在处理并发更新处理工作,我的情况是-可以从UI将一名员工标记为非活动状态,但是一旦将其标记为非活动状态,就不应再次将其标记为非活动状态。我想通过使用状态为active的条件更新实体进行db update调用来实现此目的。我需要更新的实体作为回报。

如何使用条件(where子句)更新实体并在spring数据jpa中的方法响应中获取更新的实体

employeRepository.save(employee); // is it possible to do something like this with a condition at the same time update method returns updated entity.
答案

您可以使用JPQL

uery query = entityManager.createQuery(
            "UPDATE employee SET status = "inactive"" +
                    "WHERE status="active"");
    query.executeUpdate();

或仅是春季数据

@Modifying
@Query("update employee u status = `inactive` where status=`active`")
void disableUser();

以上是关于如何使用条件(where子句)更新实体并在spring数据jpa中的方法响应中获取更新的实体的主要内容,如果未能解决你的问题,请参考以下文章