使用JPA命名查询以对象列表作为输入的命名查询

时间:2020-02-18 05:51:42

标签: jpa

当我尝试使用JPA存储库执行更新命名查询时,进入QueryExecutionRequestException。

$user = User::with('profile')->findOrFail($userId);

if ($user->profile === null)
{
    $profile = new UserProfile(['attr' => 'value', ....]);
    $user->profile()->save($profile);
}
else
{
    $user->profile()->update(['attr' => 'value', ....]);
}

代码:

org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.company.farmer.entity.FarmProducts p set p.isDeleted=:isDeleted where p.productId IN (:productIdsList_0, :productIdsList_1)]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.company.farmer.entity.FarmProducts p set p.isDeleted=:isDeleted where p.productId IN (:productIdsList_0, :productIdsList_1)]

我正在尝试传递productId列表,以将isDeleted设置为“ N”。但是由于QueryExecutionRequestException,更新功能失败。

1 个答案:

答案 0 :(得分:0)

默认情况下,Spring Data将所有查询视为SELECT语句。如果您有UPDATE(或DELETE)语句,则必须在查询上应用de macro批注。 参见https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries