Java CascadeType.REFRESH 实际上是做什么的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1403681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does CascadeType.REFRESH actually do?
提问by Kim L
What does the CascadeType.REFRESH
actually do?
什么是CascadeType.REFRESH
真正做?
The definition for it is
它的定义是
When we refresh an entity all the entities held in this field refresh too
当我们刷新一个实体时,该字段中保存的所有实体也会刷新
but what does this mean in practice? Could someone please give me a simple example?
但这在实践中意味着什么?有人可以给我一个简单的例子吗?
采纳答案by skaffman
The individual CascadeType descriptions can be a bit confusing, but there's an easy way to figure it out from the general case.
单独的 CascadeType 描述可能有点令人困惑,但有一种简单的方法可以从一般情况下弄清楚。
For any of the CascadeType
values, it means that if operation X
is called on an instance using the EntityManager
interface, and that instance has references to other entity instances, and that association has CascadeType.X
defined, then the EntityManager
operation will also be applied to that associated entity.
对于任何CascadeType
值,这意味着如果X
在使用EntityManager
接口的实例上调用操作,并且该实例具有对其他实体实例的引用,并且已CascadeType.X
定义该关联,则该EntityManager
操作也将应用于该关联实体。
So EntityManager.refresh()
is defined as :
所以EntityManager.refresh()
定义为:
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
从数据库刷新实例的状态,覆盖对实体所做的更改(如果有)。
So if entity A has a reference to entity B, and that reference is annotated with @CascadeType.REFRESH
, and EntityManager.refresh(A)
is called, then EntityManager.refresh(B)
is implicitly called also.
因此,如果实体 A 有对实体 B 的引用,并且该引用用@CascadeType.REFRESH
,注释EntityManager.refresh(A)
并被调用,则EntityManager.refresh(B)
也被隐式调用。
回答by Ethos
Retrieval by Refresh: Managed objects can be reloaded from the database by using the refresh method:
Retrieval by Refresh:可以使用 refresh 方法从数据库中重新加载托管对象:
The content of the managed object in memory is discarded (including changes, if any) and replaced by data that is retrieved from the database. This might be useful to ensure that the application deals with the most up to date version of an entity object, just in case it might have been changed by another EntityManager since it was retrieved.
内存中托管对象的内容将被丢弃(包括更改,如果有)并替换为从数据库中检索的数据。这可能有助于确保应用程序处理实体对象的最新版本,以防它在检索后可能已被另一个 EntityManager 更改。
Source: http://www.objectdb.com/java/jpa/persistence/retrieve