What does the CascadeType.REFRESH actually do? 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?
The individual CascadeType descriptions can be a confusing, but there's an easy way to figure it out from the general case.
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.
So EntityManager.refresh()
is defined as :
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.