我使用 Spring Boot、Spring Data JPA 和 Hibernate 作为持久性提供程序。我已经延长了我的Repository
与接口JPARepository
。我有一个表的实体 Bean 列表。其中一些已经存在,一些则不存在。
我想知道当我打电话时会发生什么saveAll
从我的服务层传递这个List
?
如果你看一下SimpleJpaRepository
这是一个常见的实现CrudRepository
您可以看到它只会为每个元素调用保存:
@Transactional
public <S extends T> List<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities not be null!");
List<S> result = new ArrayList<S>();
for (S entity : entities) {
result.add(save(entity));
}
return result;
}
保存本身可以区分是否persist
or merge
给定实体:
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
因此,要回答您的问题......是的,您可以在通行证列表中混合新实体和现有实体。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)