Spring Data JPA-为什么对返回的实体所做的更改会自动保留? [英] Spring Data JPA - Why are changes to a returned Entity automatically persisted?

查看:550
本文介绍了Spring Data JPA-为什么对返回的实体所做的更改会自动保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个例子来介绍这个问题.

I present the question with an example.

确认我们有一个如下所示的存储库:

Assert that we have a Repository such as the below:

public interface ExampleObjectRepository extends CrudRepository<ExampleObject, Long> {

}

通过扩展JpaRepository接口,ExampleObject存储库继承了以下方法:

By extending the JpaRepository interface, the ExampleObject repository inherits the following method:

T findOne(ID id);

现在,我观察到,如果在调用此方法后收到对ExampleObject的引用,则我对此方法所做的任何操作都会自动保存到数据库中,例如:

Now, I have observed that, if I receive a reference to an ExampleObject after a call to this method, any manipulations that I make to this method are automatically saved to the database, for example:

ExampleObject pointInCase = exampleObjectRepository.findOne(1L);
pointInCase.setName("Something else");

阅读该主题后,我了解到这表明ExampleObject实例为not detached.

Reading around the subject, I understand this this signfies that ExampleObject instance is not detached.

这违反了我的期望.我本来希望我需要使用从CrudRepository继承的save方法来保存更改:

This goes against my expectations. I would have expected that I would need to use the save method inherited from CrudRepository in order to save the changes:

T save(T entity);

有人会友好地确认从Spring Data JPA存储库返回的对象仍然是标准附件,并解释如何使用API​​标记存储库中的方法以使其仅返回分离的引用吗?

Would anyone be kind enough to confirm that objects returned from a Spring Data JPA Repository remain attached as standard, and explain how to use the API to mark a method in the repository such that it only returns detached references?

我想象当与所述save(T entity)方法一起使用时,更改实体的状态也可能会更改其定义,所以我也希望了解如何处理更新身份.

I imagine that changing the entity's state may also change its definition when it is used with said save(T entity) method, so I would also appreciate an understanding of how identity for updates are handled.

推荐答案

这是JPA的基本原则.您使用附加的(托管)实体,并且在这些托管实体上进行的所有修改都将自动变为持久性.

That's a fundamental principle of JPA. You work with attached (managed) entities, and every modification made on these managed entities is automatically made persistent.

如果您不希望更改持久化,请不要进行更改或回滚事务.

If you don't want your changes to be persistent, then don't make changes, or rollback the transaction.

在分离的实体上工作将是一场噩梦,因为它将阻止延迟加载所有关联.您总是可以在您的实体上调用EntityManager.detach(),但是我真的不会那样做.只是尝试了解它的工作原理并加以处理.好处多于弊.其中之一是,您甚至不必考虑保存复杂的业务逻辑可能进行的所有更改,因为JPA透明地为您完成了所有操作.

Working on detached entities would be a nightmare, because it would prevent lazy-loading all the associations. You can always call EntityManager.detach() on your entities, but I really wouldn't do that. Just try to understand how it works and deal with it. There are much more benefits than disadvantages. One of them being that you don't even have to think about saving all the changes that a complex business logic might do, since it's all done for you by JPA, transparently.

这篇关于Spring Data JPA-为什么对返回的实体所做的更改会自动保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆