@SequenceGenerator with allocSize> 1 generates duplicate primary keys

I use the code above to generate id:

@Id
@GeneratedValue(generator = "seqq")
@SequenceGenerator(name = "seqq", sequenceName = "seqq", allocationSize = 20, initialValue = 1)
public long getId() {
    return id;
}

I also updated persistence.xml:

   <property name="hibernate.id.new_generator_mappings" value="true"/>

and updated ddl in the database:

CREATE SEQUENCE seqq
  INCREMENT 20
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 9171
  CACHE 1;

Due to this, allocSize = 20 matches the increment value. However, I get arbitrary errors saying that a duplicate key value violates the unique "myobjects_pkey" constraint. Often this error occurs after the first save of the trial version. It seems that hibernate is trying to save the object with the same id. However, START ensures that the row is larger than any existing row in the table identifier. How to fix duplicate constraint error?

+5
source share
3 answers

try specifying a strategy as shown below:

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "seqq")

>

0

    /** The identity. */
    @Id
    @Column(name = "id", unique = true, nullable = false)
    @SequenceGenerator(name = "ID_SEQU_GENERATOR", sequenceName = "DB_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQU_GENERATOR")
    public long getId() {
       return id;
    }

DB_SEQ DB as sequence.

0

hibernate.id.new_generator_mappings = true, Hibernate id, LASTPRODUCEDID-ALLOCATIONSIZE. 50 :

:

   entity.id          SEQ last number
     48847                  48847
     48848                  48848
     48849                  48849
     48850                  48850

:

   entity.id          SEQ last number
     48801                  48851
     48802                  48852

, . allocSize, . script:

 DECLARE
    v NUMBER;
  BEGIN
    FOR r IN (select sequence_name from user_sequences) LOOP
      EXECUTE IMMEDIATE 'SELECT '|| r.sequence_name ||' .NEXTVAL FROM DUAL' INTO v;
    END LOOP;
  END;
  /
0

All Articles