When entityManager.persist(…)-Method is called and when a entityManager.merge(…) is called in spring data jpa. According to documentation:
If the entity has not been persisted yet Spring Data JPA will save the entity via a call to the entityManager.persist(…)-Method, otherwise the entityManager.merge(…)-Method will be called…
So how does spring data determines whether the entity is persisted or not?
Best Answer
here is the impl of
save
method(inSimpleJpaRepository
):So it looks at
entityInformation.isNew(entity)
. Implementation of this method is(inAbstractPersistable
) :So it decides based on id field