Hibernate generates negative id values when using a sequence

前端 未结 3 810
粉色の甜心
粉色の甜心 2020-11-29 23:00

I have a class with the following definition:

@Id
@SequenceGenerator(name = \"SEQ_ACE_WORKERS_QUEUE_STATS_ID\", sequenceName = \"SEQ_ACE_WORKERS_QUEUE_STATS_         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 23:42

    The new behaviour is the followings:

    AllocationSize is a range of primary key values reserved for Hibernate. And the select seq.nextval from dual will be done only after hibernate consumed this range of primary keys.

    So you must declare the same value on both allocationSize (Hibernate) and sequence increment by (DB)

    When explicitly set allocationSize=500, e.g. on Oracle

    create sequence SEQ_ACE_WORKERS_QUEUE_STATS_ID
           MINVALUE 1 
           MAXVALUE 999999999999999999999999999 
           START WITH 1
           INCREMENT BY 500 
           NOCACHE 
           NOCYCLE;
    

    Otherwise, you'll notice negative values or constraint errors raised from your DB because of primary key collisions.

    When the app server is restarted, you'll notice the 'jump' between the latest primary key allocated, and the 'newly' sequence number selected at restart.

    Final comment: default value is 50. So if you don't specify allocationSize on the Hibernate side, you must declare increment by 50 on the DB side.

提交回复
热议问题