Spring Data JPA

Problem

Have you ever wondered why Hibernate sometimes executes a select statement before the insert?

This happens when the Id is assigned by the program and not generated by Hibernate and @Version is not used. See Person

Solutions

To avoid this select statement there are two solutions:

  1. Either use @GeneratedValue as in PersonWithGeneratedId

  2. or if you want to assign the Id you must use @Version as in PersonWithVersion