对于Spring和Spring数据,我还很陌生,我想知道什么是更新数据库中实体的最佳实践。
我应该在我的存储库中编写自定义查询吗
@Query("update Person set email = :email where id = :id)
还是我应该使用类似
onePerson = repository.findOneById(id);
onePerson.setEmail(email);
repository.save(onePerson);
谢谢!
最佳答案
前面的@Query
示例将提供更好的性能,因为您无需下拉数据并实例化对象。但是,如果您需要下游的对象来获取其他业务逻辑,则第二种方法可能更可取。