Table: "ID" INTEGER DEFAULT nextval('zakazchik_id_seq'::regclass) NOT NULL,
Entity:

 @Id @Column(name = "\"ID\"", unique = true) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "\"zakazchik_seq_gen\"") @SequenceGenerator(name = "\"zakazchik_seq_gen\"", sequenceName = "\"zakazchik_id_seq\"") private Integer id; 

When inserting a new line writes:

  ERROR SqlExceptionHelper:131 - ERROR: duplicate key value violates unique constraint "zakazchik_new_new1_new_pk_zakaz" Подробности: Key ("ID")=(730) already exists. 

That's right, 730 id is already taken ... and so on until 743. Through hibernate, I just now started working, before that everything was through jdbc with a creepy sql query collector.
Why did he start assigning old id from 700, I told him zakazchik_id_seq ?

  • one
    What value gives on a select last_value from zakazchik_id_seq ? Also try adding @Column to the annotation - updatable = false, nullable = false and to the annotation SequenceGenerator - allocationSize=1 - Chubatiy
  • @Chubatiy 759. I do not understand. Total 743 records. - Artyom ... .- ....-.-
  • @Chubatiy yes, it helped. Can you explain why it helped? Why so strangely took the id from the sequence? - Artyom ... .- ....-.-
  • Algorithm for obtaining values. The default is 50. If you need to click every time, you need to set allocationSize=1 - Chubatiy
  • added to the response and a link to the source - Chubatiy

1 answer 1

you need to install

 @Column(name = "ID", unique = true, updatable = false, nullable = false) @SequenceGenerator(name = "zakazchik_seq_gen", sequenceName = "zakazchik_id_seq", allocationSize=1) 

By default, Hibernate uses the HiLo algorithm and reserves 50 values.

Here is a description on Habré