所以我试图配置休眠进行批处理的目的。我已经构建了一个示例应用程序,并根据Hibernates文档进行了配置。休眠批处理
但是在配置Hibernate注销SQL之后,它看起来好像没有执行批量插入,而只是单独插入。我读这本日志错了吗?
所以我在我的Spring Boot应用程序中配置了以下属性。
spring.jpa.properties.hibernate.jdbc.batch_size=10
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
这是我非常基本的批作家..
@Transactional public class BatchWriter {
private EntityManager entityManager;
private int batchSize;
public BatchWriter(EntityManager entityManager, int batchSize) {
this.entityManager = entityManager;
this.batchSize = batchSize;
}
public void writeBatchOfCustomers(int numOf) {
for(long i = 0; i <= numOf; i++) {
Customer customer = new Customer(i);
entityManager.persist(customer);
if (i % batchSize == 0) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
entityManager.flush();
entityManager.clear();
}
}
}
}
现在我正在此插入20个客户,例如在休眠日志,我看到下面的20倍:
Hibernate:
insert
into
customer
(first_name, last_name, id)
values
(?, ?, ?)
我在这里错过了什么?
它目前使用H2数据库的Spring Boot自动配置。然而,我将最终使用Spring Batch和Oracle数据库,它将使用大约35个属性插入大约30k个对象。
任何帮助表示赞赏。
感谢,
为什么在配置JPA时使用普通的SessioNFactory?你应该使用'EntityManager'而不是'SessionFactory'。 –
由于该代码已经足够复制掉休眠文档,因此它只是丢弃。我没有看到影响? – buymypies
同样,您正在配置JPA,但正在使用普通的'SessionFactory'。你正在配置X,但是使用Y.你为什么认为配置X会影响Y.如前所述,使用'EntityManager'(只需将它注入到你的作者中)并用'@ Transactional'注释这个类。删除您拥有的任何Hibernate配置,并让Spring Boot为您配置JPA。 –