mysql - 是否可以使用身份生成进行 Hibernate 批处理?

标签 mysql hibernate

我刚刚阅读了这篇文章,其中介绍了如何使用 hibernate 进行批处理,但最后它说它是通过 SEQUENCE 生成完成的。不过,我想知道 IDENTITY 生成是否可行。从周围阅读它听起来像 IDENTITY 生成是在执行 INSERT 时完成的,这意味着它可能为时已晚?

即。如果我

mgr.persist(child)
mgr.persist(parent)

在一个循环中,但我想要批处理, child 直到 mgr.flush() 才会有 id,因为插入被延迟到刷新时间。查看 IDENTITY 生成:

The only drawback is that we can’t know the newly assigned value prior to executing the INSERT statement.

所以这意味着我将不得不这样做

mgr.persist(child), mgr.persist(child), mgr.persist(child)

然后 mgr.flush() 用于 id 填充

然后

mgr.persist(parent, mgr.persist(parent), mgr.persist(parent)

我们在 mysql 中并且“可以”切换到 postgres 中,后者对序列生成和池化有更好的支持,因此它一次可以获取 50 个 ID。

我的主要问题是,是否有人使用 hibernate 和 IDENTITY 生成以及子/父关系完成批处理,以便批量处理数据?如果是这样,什么模式有效?我的上述模式有效吗?还是我错了,我可以做

mgr.persist(child), mgr.persit(parent), mgr.persist(child), mgr.persist(parent)

老实说,保留哪个更简单,因为我可以循环遍历 DTO 来保存该数据。

如果我改为使用 UUID 生成,我假设使用 Mysql 会好得多吗?那我就不用担心代码顺序了?

最佳答案

hibernate documentation说到这个限制:

It is important to realize that using IDENTITY columns imposes a runtime behavior where the entity row must be physically inserted prior to the identifier value being known.

This can mess up extended persistence contexts (long conversations). Because of the runtime imposition/inconsistency, Hibernate suggests other forms of identifier value generation be used (e.g. SEQUENCE).

There is yet another important runtime impact of choosing IDENTITY generation: Hibernate will not be able to batch INSERT statements for the entities using the IDENTITY generation.

The importance of this depends on the application-specific use cases. If the application is not usually creating many new instances of a given entity type using the IDENTITY generator, then this limitation will be less important since batching would not have been very helpful anyway.

关于mysql - 是否可以使用身份生成进行 Hibernate 批处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63422129/

相关文章:

mysql - 将现有 MySQL Rails 应用程序转换为使用 Postgres : 'all pooled connections were in use'

java - JPA2 JoinColumns JoinColumn 名称不受尊重。 (Hibernate 3.6 和 H2)

MySQL - 不显示 ID 使用的任何行

XAMPP 上的 MySQL 无法运行

mysql - 即使数据库中不存在实体,如何让 hibernate 返回实体

java - @Id @GeneratedValue 但设置自己的 ID 值

facebook - 使用 Spring Social 登录 Facebook 和 Twitter

java - postgresql jpa earth_distance 查询

php - 高效展示数据库百万记录

Mysql 5.6/5.7 : View's SELECT contains a subquery in the FROM clause