Java 使用 JPA 更新多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4069513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
updating multiple rows using JPA
提问by akp
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 keep some unchanged.
我想更新列名称为“PCNAME”的表的所有字段。我要更新的表名是 XYZ。我只想更新一些字段而不是保持一些不变。
This will affect many rows and not a single row as there will be many rows with NAME='PCNAME' How can i do it using JPA.I have entity class associated with this table.
这将影响多行而不是单行,因为会有很多行 NAME='PCNAME' 我如何使用 JPA 来做到这一点。我有与此表关联的实体类。
采纳答案by Sean Patrick Floyd
You can either do it the object oriented way or using an update query.
您可以通过面向对象的方式或使用更新查询来完成。
Object oriented:
面向对象:
public void setNameOfAllEntities(String newname){
List<MyEntity> items =
entityManager.createQuery("from MyEntity", MyEntity.class)
.getResultList();
for(MyEntity entity : items){
entity.setName(newname);
}
}
With Update Query (untested):
使用更新查询(未经测试):
public void setNameOfAllEntities(final String newname){
final int changes =
entityManager.createQuery("update MyEntity set name = :name")
.setParameter("name", newname)
.executeUpdate();
System.out.println(changes + " rows changed");
}
Obviously, the second version performs better.
显然,第二个版本性能更好。
回答by Pascal Thivent
seanizer's answeris 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:
seanizer 的答案是正确的 (+1),对于这个用例,批量更新确实很好。但是您必须对批量更新操作采取一些预防措施。解释一下 JPA 规范:
- 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:
因此,我的建议是至少增加 version 列以避免与其他线程的并发问题:
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.
并在单独的事务中或在加载任何 XYZ 之前执行它,如前所述。
References
参考
- JPA 1.0 specification
- Section 4.10 "Bulk Update and Delete Operations"
- JPA 1.0 规范
- 第 4.10 节“批量更新和删除操作”
回答by Jasper de Vries
Since Java Persistence 2.1 you can use CriteriaUpdate
to do bulk updates using the Criteria API.
从 Java Persistence 2.1 开始,您可以使用CriteriaUpdate
Criteria API 进行批量更新。
CriteriaUpdate<Entity> 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.
Criteria API 批量更新操作直接映射到数据库更新操作,绕过任何乐观锁定检查。如果需要,使用批量更新操作的便携式应用程序必须手动更新版本列的值,和/或手动验证版本列的值。持久化上下文与批量更新的结果不同步。