Hello,
Way back in Hibernate 5.1.0, I had a setup where my postgres database sequence was set to increment by 20, and my entity’s @SequenceGenerator was set with allocationSize = 1.
(Example class at end of this post)
This meant that Hibernate would call select nextval("my_entity_seq")
every time it was going to add a new row to the database.
This worked perfectly, as I need gaps of 20 in my local database rows.
However, shortly after, such as hibernate 5.1.17, and 5.4.28, this is no longer allowed - the hibernate validator says that the database sequence INCREMENT BY must match the @SequenceGenerator’s allocationSize.
And so, changing my SequenceGenerator to have allocationSize=20 causes Hibernate to grab a block of 20 sequential values, and does not call nextval() every time. The sequential values are causing conflicts during data merging.
Is there a way to have Hibernate call nextval() every time, with my sequence INCREMENT BY set to 20?
Thanks very much,
Jamie
Okay, if you must know(!), we have an application that collects copious amounts of data, and we have split the collection across multiple shard machines. We run a nightly process that merges the data from all collector shards, and generates reports.
To prevent collisions while merging the data, we have organized our sequences so that cluster 1 has indexes 1, 21, 41, etc… and cluster 2 has indexes 2, 22, 42, etc… and so on.
I suppose the alternative is to have each shard increment by 1, and have each shard have a starting value of, for example, 1E9 * the cluster index. I would rather not have to resort to that, though, as it will be a pain!
Example:
Database
----------------------------
testdb=# CREATE SEQUENCE userentity_seq START 100003 INCREMENT 20;
CREATE SEQUENCE
testdb=# CREATE TABLE userentity (
user_id BIGINT PRIMARY KEY default nextval('userentity_seq'),
username text);
CREATE TABLE
Entity Class
------------------------------
package com.test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@SequenceGenerator(sequenceName="userentity_seq", name="userentity_seq", allocationSize=1)
public class UserEntity
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="userentity_seq")
private Long user_id;
public Long getUserId() { return user_id; }
String username;
}