Issue
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?
Solution
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.
Answered By - Ori Dar