updating multiple rows using JPA

前端 未结 3 1895
我寻月下人不归
我寻月下人不归 2020-12-28 09:13

I want to update all fields of a table that has value of colum NAME as \'PCNAME\'. The table name which i want to update is XYZ.I want to update only some fields and not kee

3条回答
  •  伪装坚强ぢ
    2020-12-28 09:52

    Since Java Persistence 2.1 you can use CriteriaUpdate to do bulk updates using the Criteria API.

    CriteriaUpdate criteriaUpdate = builder.createCriteriaUpdate(Entity.class)
         .set(root.get("field"), value)
         .where(predicates);
    int updated = entityManager.createQuery(criteriaUpdate).executeUpdate();
    

    Keep in mind:

    Criteria API bulk update operations map directly to database update operations, bypassing any optimistic locking checks. Portable applications using bulk update operations must manually update the value of the version column, if desired, and/or manually validate the value of the version column. The persistence context is not synchronized with the result of the bulk update.

提交回复
热议问题