Spring Data JPA的saveAll方法为什么没有进行批量插入?

5

我正在使用一个简单的JpaRepository,调用saveAll()方法。

hibernate.jdbc.batch_size = 500
hibernate.order_inserts = true
hibernate.generate_statistics = true

运行应用程序后:

   8045055 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    137189246 nanoseconds spent preparing 1158 JDBC statements;
    1417689514 nanoseconds spent executing 1158 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    16270990 nanoseconds spent executing 1 flushes (flushing a total of 1158 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

有人能否解释一下为什么要执行0个JDBC批处理?另外,我需要指出我的实体有@GeneratedValue(strategy = IDENTITY)主键。


3
如果你使用身份标识生成器,Hibernate会在JDBC层面自动禁用插入批处理。 - Cepr0
1
你真是MVP。这应该是被接受的答案,但你发布了一个评论。 - UnguruBulan
我已经发布了答案。 - Cepr0
也许我关于批量插入的这个回答对你也很有帮助… - Cepr0
我在我的实体上使用UUID生成器,但是我使用.save()方法进行非批量插入,但仍然看到相同的问题? - ennth
3个回答

11
Hibernate在使用identity标识生成器时不执行插入批处理。有关更多信息,请单击此处

2
在我的情况下,使用Oracle时,无论使用哪种ID生成策略,都不会发生插入批处理。是的,我已经设置了属性“hibernate.jdbc.batch_size: 100”。 - raven
当我们手动设置Id时,批量插入是否有效? - Sumit Desai
文档链接似乎没有提供任何关于使用ID生成器批处理不起作用的信息。 - Jens Schauder

1

还需要设置hibernate.order_updates = true

org.hibernate.engine.jdbc.batch.internal.BatchingBatch的日志记录器更改为DEBUG,查看输出内容。


你在哪里设置这个? - ennth

1

在属性名称前面添加前缀spring.jpa.properties。应该像这样:

spring.jpa.properties.hibernate.jdbc.batch_size = 500

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接