6

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!

AndrasCsanyi
  • 3,943
  • 8
  • 45
  • 77
  • If you are inserting data through insert script you should avoid the id field insertion so that sequence will get incremented else increment it once you finished after inserting the records. – Amit Deshpande Jan 04 '13 at 07:05
  • The table does not use the sequence generated by hibernate. The table looks like this: `CREATE TABLE partners ( partner_id bigint NOT NULL, ...);` If I create one from scratch than looks like this: `CREATE TABLE partners2 ( partner_id serial NOT NULL, name character varying(255)[] )` At the first one the datatype is **bigint** and in the second one the datatype is **serial**. Can I configure somehow the generating process to create table like the second one? – AndrasCsanyi Jan 04 '13 at 09:25

2 Answers2

5

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.

nolexa
  • 2,392
  • 2
  • 19
  • 19
3

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

Community
  • 1
  • 1
Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • Hi Alex, thanks for your reply. You are right, the documentation does not have anything about @initialValue. It looks like I have to manage the test data insertions in a different way. On the other hand, I have to find a way to manage the stored procedures install during deploy. I think test data insertion will be part of it. Thanks again! – AndrasCsanyi Jan 04 '13 at 10:12