updating multiple rows using JPA

前端 未结 3 1897
我寻月下人不归
我寻月下人不归 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:46

    seanizer's answer is correct (+1) and a bulk update would be indeed nice for this use case. But you must take some precautions with bulk update operations. To paraphrase the JPA specification:

    • bulk updates bypass optimistic locking checks (so you must manually increment the version column and/or manually validate the version column if desired)
    • the persistence context is not synced with the result of bulk operations (so bulk operations should be performed in a separate transaction or at the very beginning of a transaction, before loading the state of any entity that might be affected).

    My suggestion would thus be to at least increment the version column to avoid concurrency problem with other threads:

    UPDATE XYZ xyz
    SET xyz.name = :newname, xyz.version = xyz.version + 1 
    

    And to perform it in a separate transaction or before loading any XYZ as previously explained.

    References

    • JPA 1.0 specification
      • Section 4.10 "Bulk Update and Delete Operations"

提交回复
热议问题