0

I am using JPArepository.save() to insert the record to the database but it will automatically update the existing record in the database. What I want to do is let it throw exception if there are records with same primary key in the database.

I searched the solution in Google and find a solution that said use saveAndFlush instead of save can solve it. However, it still update the existing record after I used saveAndFlush.

Michael Tsai
  • 443
  • 1
  • 7
  • 15
  • Does this answer your question? [Spring JPA / Hibernate transaction force insert instead of update](https://stackoverflow.com/questions/37253175/spring-jpa-hibernate-transaction-force-insert-instead-of-update) – M. Rizzo Feb 11 '22 at 13:57

1 Answers1

0

Best option is to do findById before save and throw exception if the Id exists.

Alien
  • 13,439
  • 5
  • 35
  • 53