【发布时间】:2022-07-30 14:56:09
【问题描述】:
我有一种情况,我需要能够更新一个实体,但如果它不存在则能够抛出异常。
我能想到的最好的:
if (!authorRepository.existsById(1L)) {
throw new Exception("entity doesn't exist yet!");
} else {
authorRepository.save(entity);
}
还有其他方法吗? JPA 似乎没有提供一种只进行更新的方法,EntityManager.merge() 的行为类似于创建或更新。
【问题讨论】:
标签: hibernate jpa spring-data-jpa spring-data