CrudRepository and Hibernate: save(List) vs save(Entity) in transaction

后端 未结 3 673
渐次进展
渐次进展 2020-12-14 06:54

Would it make any difference if I do:

@Transactional
public void processData() {
    List entities = ....;
    MyEntityRepository.save(entiti         


        
3条回答
  •  感情败类
    2020-12-14 07:25

    From SimpleJpaRepository:

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

    So, your second business method only shadows save(Iterable 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.

提交回复
热议问题