hibernate - 通过 CrudRepository 进行瞬态实体更新
问题描述
更新记录/实体时,是否必须先创建分离实体然后保存。
Entity e = entityRepository.findById(id);
// set necessary properties for update
entityRepository.save(e);
还是临时实体会做?鉴于 id 存在。
Entity e = new Entity();
// set necessary properties for update
entityRepository.save(e);
解决方案
首先我们通过 find 操作得到的实体处于持久状态而不是分离状态
这两种情况都可以,但您唯一需要注意的是您的保存语句应该有正确的事务。
推荐阅读
- c++ - 如何将类的静态方法作为 std::unique_ptr 的删除器传递?
- react-native - 使用 react-hook-form 的 React Native 应用程序不起作用(查看配置 getter 回调表单组件“输入”必须是一个函数(收到“未定义”)
- r - 在点图的底部添加条形图
- python - 来自线程 Flask 的安全数据库查询(peewee)
- c# - 从 USB 接收的数据不匹配 - Windows Form App (.NET Framework)
- php - 执行某些操作时将 IP 从 PHP 传递到 mysql
- c++ - 如何在 Linux 上使用带有 g++ 11.2 的 std::execution::par_unseq 和 std::execution::unseq 来利用向量化?
- visual-c++ - 如何简单地删除向量中的重复项?
- asp.net - 使用 HttpClient 同步调用异步方法时如何避免死锁
- list - 如何使用 Flutter Map 实现标记列表?