开发者

What's the reason behind the jumping GeneratedValue(strategy=GenerationType.TABLE) when not specifying a @TableGenerator?

Why do I need to add allocationSize=1 when using the @TableGenerator to ensure that the id wouldn't jump from 1, 2,... to 32,xxx, 65,xxx,... after a jvm restart?

Is there a design reason for the need to specify the allocationSize?

This snippet would produce the jumping ids

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
开发者_开发技巧

Here's the modified snippet that produces the properly sequenced ids

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "account_generator")
@TableGenerator(name = "account_generator", initialValue = 1, allocationSize = 1)
private Long id;


Hibernate caches a block of ids for performance reasons. It allocates several ids from database, keeps and if these run out it allocates another block from sequence (thus increasing the sequence value)


I'm not claiming it is the case but this might be a bug in the underlying generator used by Hibernate. See for example this post on Hibernate's forums that describes a weird behavior, the issues mentioned in the comments of the New (3.2.3) Hibernate identifier generators or existing issues in Jira.

My suggestion would be to identify the generator used in your case and to search for an existing issues or to open a new one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜