The object has the following annotations in the id column:
@Id @SequenceGenerator(name = "JOB_MISFIRE_ID_GENERATOR", sequenceName="job_misfire_sequence", allocationSize=10) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "JOB_MISFIRE_ID_GENERATOR") @Column(unique = true, nullable = false) private Long id;
In the database, I have the following:
CREATE SEQUENCE job_misfire_sequence INCREMENT 10 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1;
And the sequence is used to get the default value for the column.
ALTER TABLE job_misfires ALTER COLUMN id SET DEFAULT nextval('job_misfire_sequence');
When I manually insert manually into db using nextval ('job_misfire_sequence'), everything works fine. When the sequence current value is 1, the following id values were obtained:
SELECT nextval('job_misfire_sequence'); --> 1 SELECT nextval('job_misfire_sequence'); --> 11 SELECT nextval('job_misfire_sequence'); --> 21 SELECT nextval('job_misfire_sequence'); --> 31
But what happens when hibernate inserts a row into this table is that it gets the next value from this sequence (in this scenario 41) and multiplies it by 10 and uses this as the id value. This means that the inserted row now has an id value of 410.
What am I doing wrong? This situation will lead to conflicts because sleep mode does not use the value provided by the sequence. If I realized that the combination of allocationSize = 10 in the annotation and INCREMENT 10 in the sequence should ensure that in sleep mode only the new value from the sequence is required for every tenth value. Why is this not happening? Why is the value from the sequence multiplied by 10?
I use
- Postgresql 9.0.3
- Hibernate 3.5.5
- Hibernate JPA 2.0 api 1.0.0 final
Update 1:
As shown in boarding schools, setting the allocize value to 1 in the annotation solves this problem. Now the id values are really taken from the sequence in db, and I can safely insert rows manually into this table.
But:
- Does allocSize = 1 have performance issues?
- Is it not a massive mistake that the value from the sequence is not used, as indicated in sleep mode, but is multiplied by the allocize value?
- Who's guilty? Hibernate?
- Is there a fix available?