【发布时间】:2020-09-29 01:33:34
【问题描述】:
经过多年的 JEE,我目前正在实现我的第一个 Spring Boot 应用程序。
我想知道 JPA / Hibernate 的行为。在 JEE 中,一旦持久化或“找到”@Entity,对此实体的所有更改都会由 JPA 实现自动填充到数据库中,无需调用 persist()、flush() 等。
现在使用 CrudRepository 的更改只有在我明确调用 save() 时才会存储到数据库中。
我的 CrudRepository:
public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {
public Optional<UserAccount> findByEmail(String email);
public Optional<UserAccount> findByVerificationCode(String verificationCode);
}
示例代码:
public void updateUser(Long userId, String newName) {
UserAccount userAccount = userAccountRepository.findById(userId)
.orElseThrow(() -> new NotFoundException("Userid not found"));
userAccount.setLastName(newName);
//Stop here in JEE
userAccountRepository.save(userAccount);
}
在 .setLastName 之后的 JEE 实现中,更改将由 Hibernate 持久保存到数据库中。在我的 spring-boot 服务中它不是。通常明确需要发出 .save() 任何想法为什么以及如何获得与 JEE 中相同的行为?
【问题讨论】:
标签: java spring-boot hibernate jpa