如何使用Spring Data Jpa实现批量更新? 我有一个商品实体,对于差异用户级别,有差价,例如
goodsId level price
1 1 10
1 2 9
1 3 8
更新商品时我想批量更新这些价格,如下所示:
@Query(value = "update GoodsPrice set price = :price where goodsId=:goodsId and level=:level")
void batchUpdate(List<GoodsPrice> goodsPriceList);
但它会引发异常,
Caused by: java.lang.IllegalArgumentException: Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions < 8.
那么如何正确使用Spring数据Jpa实现批量更新?
答案 0 :(得分:6)
我认为根据docs,Spring Data JPA无法做到这一点。您必须查看纯JDBC,there are a few methods regarding batch insert/updates。
答案 1 :(得分:1)
如果你正在使用Hibernate,如果你愿意自己管理这些交易,你可以选择。
以下是测试示例
int entityCount = 50;
int batchSize = 25;
EntityManager entityManager = null;
EntityTransaction transaction = null;
try {
entityManager = entityManagerFactory().createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
for ( int i = 0; i < entityCount; ++i ) {
if ( i > 0 && i % batchSize == 0 ) {
entityManager.flush();
entityManager.clear();
transaction.commit();
transaction.begin();
}
Post post = new Post(String.format( "Post %d", i + 1 ) );
entityManager.persist( post );
}
transaction.commit();
} catch (RuntimeException e) {
if ( transaction != null && transaction.isActive()) {
transaction.rollback();
}
throw e;
} finally {
if (entityManager != null) {
entityManager.close();
}
}
还建议将以下属性设置为适合您需要的属性。
<property
name="hibernate.jdbc.batch_size"
value="25"
/>
<property
name="hibernate.order_inserts"
value="true"
/>
<property
name="hibernate.order_updates"
value="true"
/>
所有这些都取自以下文章。 The best way to do batch processing with JPA and Hibernate
答案 2 :(得分:1)
下面的示例:Spring Data JPA Batch Inserts中,我创建了自己的更新方式,而无需使用EntityManager。
我这样做的方法是首先检索要更新的所有数据,在您的情况下,它将是WHERE goodsId=:goodsId AND level=:level
。然后使用for循环遍历整个列表并设置所需的数据
List<GoodsPrice> goodsPriceList = goodsRepository.findAllByGoodsIdAndLevel();
for(GoodsPrice goods : goodsPriceList) {
goods.setPrice({{price}});
}
goodsRepository.saveAll(goodsPriceList);
以下某些内容是插入或更新所需的。启用generate_statistics以便您查看是否真的在批处理
// for logging purpose, to make sure it is working
spring.jpa.properties.hibernate.generate_statistics=true
// Essentially key to turn it on
spring.jpa.properties.hibernate.jdbc.batch_size=4
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
日志在这里
27315 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
603684 nanoseconds spent preparing 4 JDBC statements;
3268688 nanoseconds spent executing 3 JDBC statements;
4028317 nanoseconds spent executing 2 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
6392912 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
答案 3 :(得分:0)
要在此对话中添加另一个链接以获取更多参考
Spring Data JPA batch insert/update
尝试使用Spring Data JPA进行批量插入/更新时要考虑的几件事
答案 4 :(得分:0)
我们可以以编程方式启用查询批处理。如果需要进行大量更新以处理成千上万条记录,那么我们可以使用以下代码来实现相同的目的。
您可以定义自己的批处理大小,并调用updateEntityUtil()方法以使用Spring Data JPA或本机查询简单地触发更新查询。
代码段:-
// Total number of records to be updated
int totalSize = updateEntityDetails.size();
// Total number of batches to be executed for update-query batch processing
int batches = totalSize / batchSize;
// Calculate last batch size
int lastBatchSize = totalSize % batchSize;
// Batching-process indexes
int batchStartIndex = 0, batchEndIndex = batchSize - 1;
// Count of modified records in database
int modifiedRecordsCount = 0;
while (batches-- > 0) {
// Call updateEntityUtil to update values in database
modifiedRecordsCount += updateEntityUtil(batchStartIndex, batchEndIndex, regionCbsaDetails);
// Update batch start and end indexes
batchStartIndex = batchEndIndex + 1;
batchEndIndex = batchEndIndex + batchSize;
}
// Execute last batch
if (lastBatchSize > 0)
modifiedRecordsCount += updateEntityUtil(totalSize - lastBatchSize, totalSize - 1,
updateEntityDetails);