休眠,@SequenceGenerator 和 allocationSize [英] Hibernate, @SequenceGenerator and allocationSize

查看:17
本文介绍了休眠,@SequenceGenerator 和 allocationSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道使用 @SequenceGenerator 时 Hibernate 的默认行为 - 它将实际数据库序列增加 1,将此值乘以 50(默认 allocationSize value) - 然后将此值用作实体 ID.

这是不正确的行为并且与规范相冲突,其中说:><块引用>

allocationSize - (可选)从序列中分配序列号时要增加的数量.

需要明确的是:我不关心生成的 ID 之间的差距.

我关心与基础数据库序列不一致的 ID.例如:任何其他应用程序(例如使用普通 JDBC 的)可能想要在从序列获得的 ID 下插入新行 - 但所有这些值可能已经被 Hibernate 使用了!疯狂.

有人知道这个问题的任何解决方案吗(没有设置allocationSize=1,从而降低性能)?


把事情说清楚.如果最后插入的记录的 ID = 1,那么 HB 为其新实体使用值 51, 52, 53... 但同时:数据库中序列的值将设置为 2.当其他应用程序使用该序列时,这很容易导致错误.

另一方面:规范说(根据我的理解)数据库序列应该设置为 51 并且同时 HB 应该使用范围 2, 3 ...50


更新:
正如 Steve Ebersole 在下面提到的那样:可以通过设置 hibernate.id.new_generator_mappings=true 来启用我描述的行为(对许多人来说也是最直观的).

谢谢大家.

更新 2:
对于未来的读者,您可以在下面找到一个工作示例.

@Entity@Table(name = "用户")公共类用户{@ID@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERS_SEQ")@SequenceGenerator(name = "USERS_SEQ", sequenceName = "SEQUENCE_USERS")私人长ID;}

持久性.xml

<属性><property name="hibernate.id.new_generator_mappings" value="true"/></属性></persistence-unit>

解决方案

要绝对清楚...您所描述的内容与规范有任何冲突.该规范讨论的是 Hibernate 分配给您的实体的值,而不是实际存储在数据库序列中的值.

但是,可以选择获取您正在寻找的行为.首先看我对 的回复是有没有办法使用 JPA 注释和 Hibernate 动态选择 @GeneratedValue 策略? 这将为您提供基础知识.只要您设置为使用该 SequenceStyleGenerator,Hibernate 就会使用 SequenceStyleGenerator 中的池化优化器"来解释 allocationSize.池优化器"用于允许在创建序列时使用增量"选项的数据库(并非所有支持序列的数据库都支持增量).无论如何,请阅读那里的各种优化器策略.

We all know the default behaviour of Hibernate when using @SequenceGenerator - it increases real database sequence by one, multiple this value by 50 (default allocationSize value) - and then uses this value as entity ID.

This is incorrect behaviour and conflicts with specification which says:

allocationSize - (Optional) The amount to increment by when allocating sequence numbers from the sequence.

To be clear: I do not bother about gaps between generated IDs.

I care about IDs that are not consistent with underlying database sequence. For example: any other application (that e.g. uses plain JDBC) may want to insert new rows under IDs obtained from sequence - but all those values may be already used by Hibernate! Madness.

Do somebody know any solution to this problem (without setting allocationSize=1 and thus degrading performance)?

EDIT:
To make things clear. If last inserted record had ID = 1, then HB use values 51, 52, 53... for its new entities BUT at the same time: sequence's value in database will be set to 2. Which can easily leads to errors when other applications are using that sequence.

On the othe hand: specification says (in my understanding) that database sequence should have been set to 51 and in the meantime HB should use values from range 2, 3 ... 50


UPDATE:
As Steve Ebersole mentioned below: the behaviour described by me (and also the most intuitive for many) can be enabled by setting hibernate.id.new_generator_mappings=true.

Thanks all of You.

UPDATE 2:
For future readers, below you can find a working example.

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERS_SEQ")
    @SequenceGenerator(name = "USERS_SEQ", sequenceName = "SEQUENCE_USERS")
    private Long id;
}

persistence.xml

<persistence-unit name="testPU">
  <properties>
    <property name="hibernate.id.new_generator_mappings" value="true" />
  </properties>
</persistence-unit>

解决方案

To be absolutely clear... what you describe does not conflict with the spec in any way. The spec talks about the values Hibernate assigns to your entities, not the values actually stored in the database sequence.

However, there is the option to get the behavior you are looking for. First see my reply on Is there a way to dynamically choose a @GeneratedValue strategy using JPA annotations and Hibernate? That will give you the basics. As long as you are set up to use that SequenceStyleGenerator, Hibernate will interpret allocationSize using the "pooled optimizer" in the SequenceStyleGenerator. The "pooled optimizer" is for use with databases that allow an "increment" option on the creation of sequences (not all databases that support sequences support an increment). Anyway, read up about the various optimizer strategies there.

这篇关于休眠,@SequenceGenerator 和 allocationSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆