I have 3 rows already imported (by import.sql in class path) in my database with primary keys id=1, id=2, id=3. When I try to insert new data (through javax.faces), I get ConstraintViolationException 3 times in a row, and then, on the 4th try, new row is inserted with autogenerated id=4. Is there any possibility to avoid those 3 errors and insert data on a first try with id starting from last id in a table?

Here is my entity declaration:

@Entity
@Table(name = "book")
public class Book {

   @Id
   @GeneratedValue
   @Column(name = "id")
   private long id;
}

Thank you in advance.

1

There are 1 answers

0
anigsu On

Thank you, krokodilko. Solved it by adding strategy:

@GeneratedValue(strategy=GenerationType.SEQUENCE)

and adding this line after every table insert in import.sql script:

SELECT setval([sequencename], max(id)) FROM [tablename];