Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data : CrudRepository's save method and update

I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database like :

@Repository public interface ProjectDAO extends CrudRepository<Project, Integer> {}  @Service public class ProjectServiceImpl {  @Autowired private ProjectDAO pDAO;  public void save(Project p) { pDAO.save(p); } } 

So if I call that method on an already registred entry, it'll update it if it finds a changed attribute ?

Thanks.

like image 988
Moatez Bouhdid Avatar asked Aug 11 '16 10:08

Moatez Bouhdid


People also ask

Does CrudRepository save update?

CrudRepository has only save but it acts as update as well. When you do save on entity with empty id it will do a save .

What happens if the argument for Save () method of CrudRepository is null?

So if the field is null in the entity to save , it will overwrite its value to null in the existing row.

What does save method of CrudRepository return?

The save() method returns the saved entity, including the updated id field.

What is difference between save and Saveflush?

On saveAndFlush , changes will be flushed to DB immediately in this command. With save , this is not necessarily true, and might stay just in memory, until flush or commit commands are issued.


2 Answers

I wanted to know if the {save} method in CrudRepository do an update if it finds already the entry in the database

The Spring documentation about it is not precise :

Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.

But as the CrudRepository interface doesn't propose another method with an explicit naming for updating an entity, we may suppose that yes since CRUD is expected to do all CRUD operations (CREATE, READ, UPDATE, DELETE).

This supposition is confirmed by the implementation of the SimpleJpaRepository class which is the default implementation of CrudRepository which shows that both cases are handled by the method :

@Transactional public <S extends T> S save(S entity) {      if (entityInformation.isNew(entity)) {         em.persist(entity);         return entity;     } else {         return em.merge(entity);     } } 

So if I call that method on an already registered entry, it'll update it if it finds a changed attribute?

It will do a merge operation in this case. So all fields are updated according to how the merging cascade and read-only option are set.

like image 186
davidxxx Avatar answered Sep 25 '22 08:09

davidxxx


Looking at the default implemantation of CrudRepository interface

 /*      * (non-Javadoc)      * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)      */     @Transactional     public <S extends T> S save(S entity) {          if (entityInformation.isNew(entity)) {             em.persist(entity);             return entity;         } else {             return em.merge(entity);         }     } 

Save method manage two situations:

-If the person Id is null (a new entity is created) then save will call persist method => insert query will be executed.

-If the person id is not null then save will call merge: fetch the existing entity from entityManagerFactory(from the 2 level cache if it doesn't exist then it will be fetched from the database) and comparing the detached entity with the managed and finally propagate the changes to the database by calling update query.

like image 39
SEY_91 Avatar answered Sep 23 '22 08:09

SEY_91