1

In my spring project, the tables in database are created automatically by Hibernate using my entity classes as base, but I insert some default values in the table manually (using pgAdmin3).

Because that, I am facing now this problem: when I try insert a value via Java code in one of the tables which already have values, I receive a error message, saying the primary key already exists in the database.

Anyone knows how to solve this problem?

UPDATE

That's how I declare my primary key in my class:

@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Kleber Mota
  • 8,521
  • 31
  • 94
  • 188

2 Answers2

3

Call this SQL query once per table to set the sequence to the next free number:

SELECT setval('tblname_id_seq', max(id)) FROM tblname;

tblname being the actual name of the table.

Hibernate may use a different naming convention, or the sequence may have been renamed. If you can't find the sequence behind the serial column, check with (per documentation):

SELECT pg_get_serial_sequence(tblname, column_name)

More details:
Modify Django AutoField start value
How to import a CSV to postgresql that already has ID's assigned?

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • What I understand here it's your example works to update this tblname_id_seq in the postgresql server. But I don't have this sequence, my primary keys are handled only by Hibernate. – Kleber Mota May 22 '14 at 23:34
  • @KleberMota: Serial numbers are typically handled by the RDBMS. There is a sequence object behind each `serial` column. Follow the links to the manual for more. I added a method to find the sequence. – Erwin Brandstetter May 23 '14 at 13:22
  • hibernate_sequence is name of the sequence that hibernate uses by default. Try o look for this sequence in in pgAdmin3 use it in the query above. – Nikhil Mahajan Jun 16 '14 at 08:07
0

The problem here might be that you declare the id as a primitive instead of a wrapper.

So instead of:

private int id;

You should have:

private Integer id;

When you create the entity with the id is initialized as 0, instead of NULL.

That's why you get duplicate id constraint violation exceptions.

Only when the id is NULL the AUTO generation strategy will delegate the id assignment to the database.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I update my code to change my Id as Integer, but still have the same problem (by the way, the entity isn't being initialized with 0, but with the last number in a sequence of numbers - in the moment the last number is 9 - , which match the number of elements I insert through the program, beacause that I think the Hibernate had your own counter). – Kleber Mota May 23 '14 at 10:40
  • [According to Hibernate docs](http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch04.html#persistent-classes-pojo-identifier): "We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type." – Vlad Mihalcea May 23 '14 at 10:57
  • As for the default values being manually inserted, make sure you always use the sequence when inserting those and never assign values manually. Better to have your PRIMARY KEY set as: id bigint NOT NULL DEFAULT nextval('my_table_sequence'::regclass) – Vlad Mihalcea May 23 '14 at 10:59