0

I am developping a java application with postgresql database, I made a manual insert into a table, and now when i try to insert from the application I get the following error:

Detail: Key (id)=(1092770) already exists

I am using eclipseLink as JPA framework and the Id generation strategy is the following:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
protected long id = 0L;

Clearly the eclipseLink is not considering the max(Id) in the table, and creates an Id less than the current max(Id).

Is there a way to fix this please

Thanks

rainman
  • 2,551
  • 6
  • 29
  • 43
  • If you are inserting rows up to/including row id 1092770, you should also insert that value into the sequence, and should define that start value in your sequence object (see initialValue in SequenceGenerator https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Entities/Ids/SequenceGenerator ). As this is a one time operation, having it query and set a value each time a persistence unit starts isn't ideal: I don't believe there are automated api for it so you'd have to write your own – Chris Jun 02 '22 at 20:42
  • Thank you for your reply, so if I understood well, i have to change id generating strategy to SEQUENCE right ? – rainman Jun 07 '22 at 15:32
  • No, you just need what ever strategy you use to start with an initial value higher than any data you might already have in your table. GenerationType auto with EclipseLink will use Table sequencing with a table 'sequence'. You can insert the initial value in that table yourself, or define the table generator with the initial value directly: https://docs.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html – Chris Jun 07 '22 at 16:37

0 Answers0