JPQL - Update with Multiple where conditions

前端 未结 1 751
余生分开走
余生分开走 2021-01-28 16:24

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,
                


        
相关标签:
1条回答
  • 2021-01-28 16:58

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

    0 讨论(0)
提交回复
热议问题