Del*_*fie 1 java hibernate spring-data-jpa spring-boot
我正在使用 spring data JPA 将数据保存到数据库中。CRUDRepository 有saveAll(Iterable)我正在使用的
由于我对实体字段有唯一的约束,因此我想知道如果我尝试保存包含违反此约束的对象的 Interable 会发生什么。
使用标准saveAll方法 from SimpleJpaRepository,您基本上会多次调用该save方法,每个实例一次
@Transactional
public <S extends T> List<S> saveAll(Iterable<S> entities) {
// ... Omitted
for (S entity : entities) {
result.add(save(entity));
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以看到一个EntityManager实例被使用,调用persist或merge。
这意味着它的规则适用,并且您将收到ConstraintViolationException。
| 归档时间: |
|
| 查看次数: |
6596 次 |
| 最近记录: |