CascadeType.REFRESH实际上做了什么?

时间:2023-01-24 23:07:13

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?

但这在实践中意味着什么?有人可以给我一个简单的例子吗?

2 个解决方案

#1


98  

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值,这意味着如果使用EntityManager接口在实例上调用操作X,并且该实例具有对其他实体实例的引用,并且该关联已定义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)。

#2


4  

Retrieval by Refresh: Managed objects can be reloaded from the database by using the refresh method:

通过刷新检索:可以使用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

资料来源:http://www.objectdb.com/java/jpa/persistence/retrieve

#1


98  

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值,这意味着如果使用EntityManager接口在实例上调用操作X,并且该实例具有对其他实体实例的引用,并且该关联已定义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)。

#2


4  

Retrieval by Refresh: Managed objects can be reloaded from the database by using the refresh method:

通过刷新检索:可以使用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

资料来源:http://www.objectdb.com/java/jpa/persistence/retrieve