Cascade Types

JPA Entity Objects

(referenced from https://www.objectdb.com/java/jpa/persistence/managed and https://sites.google.com/site/mostlyjava/scbcd/06-java-persistence-entity-operations, click the link to find more details)

In TIS, we use JPA Entity Objects (in-memory instances) to do many operations on the database, including storing, retrieving, updating and deleting database objects.

Entity Instance’s Life Cycle

The life cycle of entity objects consists of 4 states: New, Managed, Removed and Detached, as is shown in the graph below:

In brief:

persiste() → save to the db

merge() → merge the current instance with the one retrieved from the db, then save to the db

refresh() → get the latest state and records

remove() → remove from the db


More detailed semantics of the operations are below:

A new entity instance becomes both managed and persistent by invoking the persist method on it or by cascading the persist operation.

The semantics of the persist operation, applied to an entity X are as follows:

  • If X is a new entity, it becomes managed. The entity X will be entered into the DB at or before transaction commit or as a result of the flush operation.

  • If X is a preexisting managed entity, it is ignored by the persist operation.

  • If X is a removed entity, it becomes managed.

  • If X is a detached object, the EntityExistsException may be thrown when the operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

  • For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with cascade=PERSIST or cascade=ALL (or specified with the equivalent XML descriptor element), the persist operation is applied (cascaded) to Y.

 

A managed entity instance becomes removed by invoking the remove method on it or by cascading the remove operation.

The semantics of the remove operation, applied to an entity X are as follows:

  • If X is a new entity, it is ignored by the remove operation. However, the remove operation is cascaded to entities referenced by X, if the relationship from X to these other entities is annotated with cascade=REMOVE or cascade=ALL.

  • If X is a managed entity, the remove operation causes it to become removed. The remove operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with cascade=REMOVE or cascade=ALL.

  • If X is a detached entity, an IllegalArgumentException will be thrown by the remove operation (or the transaction commit will fail).

  • If X is a removed entity, it is ignored by the remove operation.

  • A removed entity X will be removed from the database at or before transaction commit or as a result of the flush operation.

 

The semantics of the flush operation, applied to an entity X are as follows:

  • If X is a managed entity, it is synchronized to the database.

For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with cascade=PERSIST or cascade=ALL, the persist operation is applied to Y.

Otherwise (no cascade or cascade is either MERGE, REMOVE or REFRESH):

If Y is new or removed, an IllegalStateException will be thrown by the flush operation (and the transaction rolled back) or the transaction commit will fail.

If Y is detached, the semantics depend upon the ownership of the relationship. If X owns the relationship, any changes to the relationship are synchronized with the database; otherwise, if Y owns the relationships, the behavior is undefined. (X can reach Y means the relationship exists, changes to the relationship must mean the relationship is NEWLY ESTABLISHED

(So, to be safe) Y should be explicitly persisted before flush operation if cascade is not PERSIST of ALL.

  • If X is a removed entity, it is removed from the database. No cascade options are relevant.

 

The merge operation allows for the propagation of state from detached to managed:

The semantics of the merge operation applied to an entity X are as follows:

  • If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.

  • If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.

  • If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).

  • If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation.

  • For all entities Y referenced by relationships from X having cascade=MERGE or cascade=ALL, Y is merged recursively as Y'. For all such Y referenced by X, X' is set to reference Y'.

  • If X is an entity merged to X', with a reference to another entity Y, where cascade=MERGE or cascade=ALL is NOT specified, then navigation of the same association from X' yields a reference to a managed object Y' with the same persistent identity as Y.

Cascade types

(Some examples can found in the link: https://howtodoinjava.com/hibernate/hibernate-jpa-cascade-types/)

  1. CascadeType.PERSIST : cascade type presist means that save() or persist() operations cascade to related entities.

  2. CascadeType.MERGE : cascade type merge means that related entities are merged when the owning entity is merged.

  3. CascadeType.REFRESH : cascade type refresh does the same thing for the refresh() operation.

  4. CascadeType.REMOVE : cascade type remove removes all related entities association with this setting when the owning entity is deleted.

  5. CascadeType.DETACH : cascade type detach detaches all related entities if a “manual detach” occurs.

  6. CascadeType.ALL : cascade type all is shorthand for all of the above cascade operations.