【发布时间】:2019-05-02 10:50:30
【问题描述】:
我正在尝试批量更新表格。 以下是我的配置:
spring.jpa.properties.org.hibernate.flushMode=COMMIT
spring.jpa.properties.hibernate.jdbc.batch_size=10
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.org.hibernate.SQL=DEBUG
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
以下是实际使用的方法:
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void updateEmployeeDetails(Map<String, List<String>> orgEAMap) {
....
updateEmployee(employeeMap);
}
public void updateEmployee(Map<String, String> employeeMap) {
//int i =0;
for (Entry<String, String> mapEntry : employeeMap.entrySet()) {
Query query = em.createNativeQuery("UPDATE employee emp SET emp.name = :name WHERE emp.id = :id");
query.setParameter("id", mapEntry.getValue());
query.setParameter("name", mapEntry.getKey());
query.executeUpdate();
//if(i==10){
//LOG.info("Flushmode"+em.getFlushMode());
//em.flush();
//}
//i++;
}
}
我尝试在某个计数后手动刷新,但在每次查询执行后刷新(部分刷新)已经发生。 从统计日志中,我可以看到有 10 个语句正在创建,0 个批处理和刷新正在发生。
【问题讨论】:
标签: hibernate jpa spring-data-jpa batch-processing updates