JQPL Update Query to update entity without using the primary key

柔情痞子 提交于 2019-12-02 11:20:47

You cannot do it in the exact way you describe - by passing an entity reference, but you can use bulk queries to achieve the same effect.

UPDATE Customer c SET c.name = :name WHERE c.uniqueExternalId = :externalId

Please note that you will have to explicitly define each updated attribute.

It is important to note that bulk queries bypass the persistence context. Entity instances that are managed within the persistence context will not reflect the changes to the records that are changed by the bulk update. Further, if you use optimistic locking, consider incrementing the @Version field of your entities with the bulk update:

 UPDATE Customer c SET c.name = :name, c.version = c.version + 1  WHERE c.uniqueExternalId = :externalId

EDIT: The JPA 2.0 spec advises in § 4.10:

In general, bulk update and delete operations should only be performed within a transaction in a new persistence context or before fetching or accessing entities whose state might be affected by such operations.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!