gpt4 book ai didi

java - Spring hibernate jdbc 批量大小

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:34:05 25 4
gpt4 key购买 nike

我认为 hibernate documenation 中的一些场景有点不清楚.

数据类:

class HibernateDao {
// ...

@Transactional
public void store(List<Object> batch) {
for(Object o : batch) {
hibernate.store(o);
}
}
}

场景一

hibernate.jdbc.batch_size = 1

Q1:当使用 10 项的集合调用 store(..) 时会发生什么?会有 10 x 1 笔交易还是只有一笔交易?


场景2

hibernate.jdbc.batch_size = 10

Q2:当使用 1 项的集合调用 store(..) 时会发生什么?无论 batch_size 属性如何,它都会立即写入后备存储吗?


来自 hibernate 文档:

Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator

Q3:什么被归类为identify identifier generator,使用注解@Id@GeneratedValue(strategy = GenerationType .AUTO)?

最佳答案

Q1: What happens when invoking store(..) with a collection of 10 items? Will there be 10 x 1 transactions or only one?

这是特定于 Spring 事务的,但就 Hibernate 而言,如果您使用一个 session 和一个“事务”,它会等到“刷新”或“提交”才能实际执行操作。所以,一笔交易。

Q2: What happens when invoking store(..) with a collection of 1 items? Will it immediately be written to the backing store regardless of the batch_size property?

不是马上。与之前的响应相同:除非您明确要求“刷新”,否则 Hibernate 将在提交阶段执行操作。

Q3: What is classified as an identify identifier generator, using the annotations @Id and @GeneratedValue(strategy = GenerationType.AUTO)?

Hibernate 无法预测为 ID 的任何内容。例如,身份(类似于序列,但对于 T-SQL 数据库)、自动递增、序列……原因很简单:Hibernate 不知道为每个批处理实体生成的 ID 是什么,因此,实体的状态 after insert 将与插入之前的状态不同。在常规情况下,Hibernate 通过调用“getGeneratedKeys”的 JDBC 方法来处理该问题,这允许它同步数据库中的数据与其 session 中的数据,但不可能批量执行此操作。

如果 Hibernate 确实知道实体的 ID(即:assigned、hilo、uuid 等),它可以安全地执行批处理。

关于java - Spring hibernate jdbc 批量大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5299955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com