What is the reason for GeneratedValue jumps (strategy = GenerationType.TABLE) when @TableGenerator is not specified?

Why do I need to add allocSize = 1 when using @TableGenerator to ensure that id will not go from 1, 2, ... to 32, xxx, 65, xxx, ... after restarting jvm

Is there a design reason for specifying allocSize?

This snippet will create jumping identifiers

@Id @GeneratedValue(strategy = GenerationType.TABLE) private Long id; 

Here's a modified snippet that creates correctly ordered identifiers

 @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "account_generator") @TableGenerator(name = "account_generator", initialValue = 1, allocationSize = 1) private Long id; 
+4
source share
2 answers

Hibernate caches an identifier block for performance reasons. It extracts several identifiers from the database, saves, and if they end, it allocates another block from the sequence (thereby increasing the value of the sequence)

+2
source

I am not saying that this is so, but it may be a mistake in the base generator used by Hibernate. See for example this post on the Hibernate forums for strange behavior, issues mentioned in the comments New (3.2.3) hibernation identifier generators, or existing problems in Jira .

My suggestion is to identify the generator used in your case and look for existing problems or open a new one.

0
source

Source: https://habr.com/ru/post/1310708/


All Articles