Update or SaveorUpdate in CRUDRespository, Is there any options available
The implementation of the method
<S extends T> S save(S entity)
from interface
CrudRepository<T, ID extends Serializable> extends Repository<T, ID>
automatically does what you want. If the entity is new it will call persist
on the entity manager
, otherwise it will call merge
The code looks like this:
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
and can be found here. Note that SimpleJpaRepository
is the class that automatically implements CrudRepository
in Spring Data JPA.
Therefore, there is no need to supply a custom saveOrUpdate()
method. Spring Data JPA has you covered.