Hibernate,@SequenceGenerator和allocationSize [英] Hibernate, @SequenceGenerator and allocationSize
问题描述
当使用 @SequenceGenerator
时,我们都知道Hibernate的默认行为 - 它通过一个增加实际的数据库序列,将该值乘以50默认分配大小
值) - 然后使用此值作为实体ID。
这是不正确的行为,说: allocationSize - (可选)从序列中分配序列号时要增加的数量。 要清楚:我不打扰生成的ID之间的差距。 我关心与基础数据库序列不一致的ID。例如:任何其他应用程序(例如使用普通JDBC)可能需要在从序列获得的ID下插入新行 - 但所有这些值可能已被Hibernate使用!疯狂。 有人知道这个问题的任何解决方案(没有设置 编辑: 感谢您的所有。 更新2: persistence.xml 不以任何方式与规范冲突。该规范讨论了Hibernate分配给实体的值,而不是实际存储在数据库序列中的值。 但是,可以选择获取您正在查看的行为对于。首先查看我对的回复有一种方法可以使用JPA批注和Hibernate动态选择@GeneratedValue策略?这将为您提供基本知识。只要您设置为使用SequenceStyleGenerator,Hibernate就会使用SequenceStyleGenerator中的pooled optimizer解释 We all know the default behaviour of Hibernate when using 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 EDIT: On the othe hand: specification says (in my understanding) that database sequence should have been set to Thanks all of You. UPDATE 2: persistence.xml
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 这篇关于Hibernate,@SequenceGenerator和allocationSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
allocationSize = 1
并因此降低性能)?
清楚地说明问题。
如果最后插入的记录的ID = 1
,那么HB使用值 51,52,53 ...
为其新实体BUT同时:序列在数据库中的值将被设置为 2
。当其他应用程序使用该序列时,这很容易导致错误。另一方面:规范说(根据我的理解)数据库序列应该被设置为 51 $ c $ c>,同时HB应该使用
2,3 ... 50
$ b 范围内的值/> UPDATE:
正如Steve Ebersole所述:我描述的行为(也是最直观的行为)可以通过设置 hibernate.id.new_generator_mappings = true
。
对于未来的读者,您可以在下面找到一个工作示例。
@ @
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator)
@Table(name =users)
public class User {
@Id
@GeneratedValue =USERS_SEQ)
@SequenceGenerator(name =USERS_SEQ,sequenceName =SEQUENCE_USERS)
private Long id;
< persistence-unit name =testPU>
<属性>
< property name =hibernate.id.new_generator_mappingsvalue =true/>
< / properties>
< / persistence-unit>
allocationSize
。 合并优化器适用于在创建序列时允许增量选项的数据库(并非所有支持序列的数据库都支持增量)。无论如何,请阅读那里的各种优化策略。 @SequenceGenerator
- it increases real database sequence by one, multiple this value by 50 (default allocationSize
value) - and then uses this value as entity ID.
allocationSize=1
and thus degrading performance)?
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. 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
.
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-unit name="testPU">
<properties>
<property name="hibernate.id.new_generator_mappings" value="true" />
</properties>
</persistence-unit>
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.