AndrasCsanyi
AndrasCsanyi

Reputation: 4255

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

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!

Upvotes: 6

Views: 12950

Answers (2)

nolexa
nolexa

Reputation: 2492

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.

Upvotes: 5

Alex Gitelman
Alex Gitelman

Reputation: 24722

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

Upvotes: 3

Related Questions