Java – should hibernate use a unique sequence for each table?
I have several entities using automatic key generation strategies with hibernate and Postgres
@Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id;
This will cause hibernate to be generated_ Sequence, and each entity will use the sequence when assigning keys
Now I have a table that contains a lot of cached data (such as 100k entries) and some user tables Since both use the policy auto, they both obtain keys from the same sleep sequence As a result, even if I have only 10 users, they will all have 6-7-bit IDS, such as 123123
I wonder, in general, whether you should introduce custom sequences for each table? Or should I not care so much?
Solution
I recently solved this problem for my project I use the enhanced sequence generator (which is the default value of the sequence style generator) and set the preference_ sequence_ per_ Set the entity parameter to true
My package info Java content:
@GenericGenerator( name = "optimized-sequence",strategy = "enhanced-sequence",parameters = { @Parameter(name="prefer_sequence_per_entity",value="true"),@Parameter(name="optimizer",value="hilo"),@Parameter(name="increment_size",value="50")}) package org.example.model; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Parameter;
In terms of use, you only need
@Id @GeneratedValue(generator="optimized-sequence") public long id;
I prefer a separate sequence because occasionally I delete a table and recreate it. I want the ID to start with one