Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrudRepository and Hibernate: save(List<S>) vs save(Entity) in transaction [closed]

Would it make any difference if I do:

@Transactional public void processData() {     List<MyEntity> entities = ....;     MyEntityRepository.save(entities); } 

vs.

@Transactional public void processData() {     List<MyEntity> entities = ....;     for (MyEntity entity : entities) {         MyEntityRepository.save(entity);     } } 

What is the difference in terms of the underlying queries and performance?

like image 998
BPm Avatar asked Sep 26 '15 03:09

BPm


People also ask

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.

What is the difference between save and saveAll in JPA?

In our case, each time we call the save() method, a new transaction is created, whereas when we call saveAll(), only one transaction is created, and it's reused later by save().

How do you save a list of entities in Spring data JPA?

For SpringData Jpa, a cleaner approach will be to use repository. saveAll instead of a for loop with repository. save . saveAll will automatically iterate through the list and save it.

What is the difference between a CrudRepository and a JpaRepository What is the difference between a CrudRepository and a JpaRepository?

Crud Repository is the base interface and it acts as a marker interface. JPA also provides some extra methods related to JPA such as delete records in batch and flushing data directly to a database. It provides only CRUD functions like findOne, saves, etc. JPA repository also extends the PagingAndSorting repository.


2 Answers

From SimpleJpaRepository:

@Transactional public <S extends T> List<S> More save(Iterable<S> entities) {      List<S> result = new ArrayList<S>();      if (entities == null) {         return result;     }      for (S entity : entities) {         result.add(save(entity));     }      return result; } 

So, your second business method only shadows save(Iterable<S> entities) Crud Repository method, in the sense that it iterates the list and calls save(S) on your behalf.

As long as transaction is demarcated from your processData business method, there is no really a difference in performance or queries executed.

like image 87
Ori Dar Avatar answered Sep 18 '22 08:09

Ori Dar


As what has been mentioned by Ori Dar, there is no really a difference.

However, there is one thing you should notice:the method used to a save a list of elements has been renamed into <S extends T> List<S> saveAll(Iterable<S> entities) in 2.2.0.M1 according to the repo history, and the save method no longer takes as argument a list.

Since I don't have 50 reputation to comment the answer or question above, I have to write a new answer about this change.

like image 33
kayochin Avatar answered Sep 21 '22 08:09

kayochin