views:

152

answers:

2

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;
+2  A: 

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)

Priit
just be aware you are taking a performance hit for persisting objects when using a block size of 1
Justin
A: 

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.

Pascal Thivent