@SequenceGenerator allocize "duplicate keyword error"

I have a SequenceGenerator with a Size distribution of 5 (along with hibernate.id.new_generator_mappings = true). So from my understanding of hibernate one should try and return these values ​​as a new id:

    • a) get next value from database sequence (e.g. id = 70)
    • b) subtract the value of allocize (70-5 = 65)
    • c) return 65 + 1 = 66 as the new identifier.
  • returns 67 as a new identifier.

  • return 68
  • return 69
  • return 70
  • the last value in the sequence is horrible, so continue with 1a)

So ids should be in the range 66 to 70. In my case the problem is that the generated ids are 65 to 69. If I call next_value from the database (postgres) and the sequence returns 65, I get a duplicate key error when hibernate generates 65 too late (after getting 70 from sequence).

Any hints or help on this topic?

€: Sorry, forgot to leave my code:

Generator:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = GENERATOR_NAME)
@SequenceGenerator(sequenceName = "hibernate_sequence", name = GENERATOR_NAME, allocationSize = 5)
private Integer id;

      

Sequence (Postgresql):

CREATE SEQUENCE hibernate_sequence
  INCREMENT 5
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 10041865
  CACHE 1;

      

+3


source to share


2 answers


You need to use the pooled-lo optimizer as it can interoperate with other systems, and also the inserts issued by the admin console:



@Id
@GenericGenerator(name = "sequenceGenerator", strategy = "enhanced-sequence",
    parameters = {
        @org.hibernate.annotations.Parameter(
            name = "optimizer", 
            value = "pooled"),
        @org.hibernate.annotations.Parameter(
            name = "initial_value", 
            value = "1"),
        @org.hibernate.annotations.Parameter(
            name = "increment_size", 
            value = "5")
    }
)
@GeneratedValue(
    strategy = GenerationType.SEQUENCE, 
    generator = "sequenceGenerator")
private Long id;

      

+1


source


Change the hbm2ddl.auto config, set it to create

, then it will work as the sequence value used to assign the id in the table depends on the previously stored sequence number, not an error.



<property name="hbm2ddl.auto">create</property> 

      

-1


source







All Articles