Here is the code:
@Repository
public interface AccountRepository extends JpaRepository {}
JpaRepository from
The save(…)
method of the CrudRepository
interface is supposed to abstract simply storing an entity no matter what state it is in. Thus it must not expose the actual store specific implementation, even if (as in the JPA) case the store differentiates between new entities to be stored and existing ones to be updated. That's why the method is actually called save(…)
not create(…)
or update(…)
. We return a result from that method to actually allow the store implementation to return a completely different instance as JPA potentially does when merge(…)
gets invoked.
So generally it's more of an API decision to be lenient (permissible, tolerant) regarding the actual implementation and thus implementing the method for JPA as we do. There's no additional proxy messaging done to the entities passed.
You missed the second part: if the entity isn't new, merge
is called. merge
copies the state of its argument into the attached entity with the same ID, and returns the attached entity. If the entity isn't new, and you don't use the returned entity, you'll make modifications to a detached entity.