JPQL - 更新与多个条件 [英] JPQL - Update with Multiple where conditions

查看:146
本文介绍了JPQL - 更新与多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPQL是否支持以下更新?

 更新人员p set p.name =:name_1其中p.id =: id_1,
p.name =:name_2其中p.id =:id_2,
p.name =:name_3其中p.id =:id_3

如果不是,是否有其他选择?
我自己尝试过。但是 createQuery 返回null。 解决方案

JPA 2.0支持大小写表达式。您可以使用来尝试JPQL查询, CASE



Update Person p set p.name = CASE WHEN(p.id =:id1)THEN: name_1 WHEN(p.id =:id2)THEN:name_2 WHEN(p.id =:id3)THEN:name_3 END


general_case_expression :: = CASE WHEN conditional_expression THEN
scalar_expression ELSE scalar_expression END


  • 尝试使用Criteria API来构建查询。



    lockquote>

    when(表达式条件,R结果):当/ then子句添加
    时(b),(b),(b),(b)和(b)中的表达式。 id),id1),name_1);



  • Does JPQL support the following Update?

    Update Person p set p.name = :name_1 where p.id = :id_1,
                        p.name = :name_2 where p.id = :id_2,
                        p.name = :name_3 where p.id = :id_3
    

    If not, are there any alternatives? I tried it myself. But createQuery returned null.

    解决方案

    Case expression is supported in JPA 2.0. I have provided pseudo-code, can make modifications accordingly.

    • You can try below JPQL query using CASE.

      Update Person p set p.name = CASE WHEN (p.id = :id1) THEN :name_1 WHEN (p.id = :id2) THEN :name_2 WHEN (p.id = :id3) THEN :name_3 END

      general_case_expression::= CASE WHEN conditional_expression THEN scalar_expression ELSE scalar_expression END

    • Else, can try Criteria API to build the query.

      when(Expression condition, R result) : Add a when/then clause to the case expression.

      criteriaBuilder.selectCase().when(criteriaBuilder.equal(person.get("id"), id1), name_1);

    这篇关于JPQL - 更新与多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆