【发布时间】:2021-02-20 01:37:57
【问题描述】:
大家早上好, 我试图通过它的 id 完全更新一个实体,我正在使用一个从 JPA 扩展的存储库,我认为只要让它保存系统就会破坏旧的系统,但事实并非如此,我收到了这个错误:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
我尝试过使用 entityManager.flush();但我得到一个错误:
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'flush' call
我找到了更新数据库字段的选项,但我真正想要的是完全粉碎数据库行,有那么复杂吗?我该怎么做?我应该删除记录并重新创建它吗? (我不相信,因为我们正在生成两个过渡,但我不知道它是否正常)
我把负责修改实体的代码放上去:
@PostMapping ("/ user")
public ResponseEntity postUser (@RequestBody User user) {
Map <Object, Object> model = new HashMap <> ();
entityManager.flush ();
User newUSer = userRepository.saveAndFlush (user);
model.put ("user", newUSer);
return ok (model);
}
谢谢!
【问题讨论】:
标签: spring-boot hibernate jpa repository