1

I have an existing db with the following schema and sequence:

CREATE TABLE public.my_table
(
  id bigint NOT NULL,
  ...
  CONSTRAINT travelit_hotels_pkey PRIMARY KEY (id)
);

CREATE SEQUENCE public.my_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 500791
  CACHE 1;

My @Entity mapping makes use of the sequence:

@Id
@GeneratedValue(generator = "my_seq")
@SequenceGenerator(name = "my_seq", sequenceName = "my_seq")
private Long id;

As of hibernate-5, I'm using the following property: hibernate.id.new_generator_mappings=true

Result: when I create a new entity, it gets the following id assigned: 500744.

Which is < START of the SEQUENCE, and already exists in my postgres DB!

Why?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • the [answer](http://stackoverflow.com/questions/4288740/hibernate-use-of-postgresql-sequence-does-not-affect-sequence-table) here , wasnt helpfull ? – AntJavaDev Aug 09 '16 at 10:28

2 Answers2

0

When using hibernate.id.new_generator_mappings = true, hibernate would by default create a sequence with INCREMENT 50.

As described here, the generated id will then be SEQ-49. This creates conflicts with existing databases that have been generated with hibernate < 5.

Solution: limit the allocationSize to 1 for your existing sequence generators: @SequenceGenerator(.., allocationSize = 1)

membersound
  • 81,582
  • 193
  • 585
  • 1,120
0

proper annotations should be:

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_generator")
@SequenceGenerator(name="my_generator", sequenceName="my_seq", allocationSize=1, initialValue=1)

if you just have sequence on database, hibernate not override it. To change suquence you can delete from database and change in hibernate.cfg.xml

<property name="hibernate.hbm2ddl.auto">update</property>

update to update daatabase or create to drop all and create again (tables and sequences)

Victor1125
  • 642
  • 5
  • 16