Generated sequence starts with 1 instead of 1000 which is set in annotation

谁说我不能喝 提交于 2019-12-12 09:38:41

问题


I would like to ask some help regarding database sequence created by Hibernate.

I have this annotation - the code below - in my entity class in order to have individual sequence for partners table. I expect that the sequence starts with 1000, because I insert test data into my database using import.sql during deploy and I would like to avoid the constraint violation. But when I want to persist data than I got the constraint violation exception and it informs me about the fact the partner_id = 2 already exists. It looks like I missed something.

    @Id
    @Column(name = "partner_id")
    @SequenceGenerator(initialValue=1000, 
                        allocationSize=1,
                        name = "partner_sequence", 
                        sequenceName="partner_sequence")
    @GeneratedValue(generator="partner_sequence")
    private Long partnerId;

The generated sequence looks like this:

CREATE SEQUENCE partner_sequence
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;
ALTER TABLE partner_sequence
  OWNER TO postgres;

I use postgres 9.1.

Did I miss something? This is the way how can I approach what I want?

Thanks for any help in advance!


回答1:


initialValue and alocattionSize are specific to hilo algorithm that uses sequence. According to this initialValue is not even supported. I don't even see how it could be supported from Java layer since sequence values are generated in the database.

Also see hibernate oracle sequence produces large gap




回答2:


initialValue is supported if hibernate.id.new_generator_mappings=true is specified according to this article. I had the same problem as stated in this post, and I solved it following this recipe. Sequences are generated correctly now.



来源:https://stackoverflow.com/questions/14152348/generated-sequence-starts-with-1-instead-of-1000-which-is-set-in-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!