There are six types of Scades (Cascadetype) in the specification of JPA. It’s them:
- ALL = Performs all cascade operations
- DETACH = Performs detach cascading operation
- MERGE = Perform the cascade merge operation
- PERSISTS = Performs the persist cascade operation
- REFRESH = Performs the refresh cascade operation
- REMOVE = Performs cascade remove operation
Cascading operations only make sense in relationships Father - Son (the transition of the state of the Father entity being performed cascading in the Son entity). As much as it is allowed to map the Cascade in reverse (Son - Father) is not very useful and cannot be considered a good practice.
- The explanation of the operation of these types of Scade is correct?
- When the MERGE is executed, it also persists the children if they have not yet been persisted.
- REFRESH unsaved, but updates the entity with the bank information.
- MappedBy mapping should only place the Scade on the parent object?
- It is good practice that cascading operations are done father to son, not the other way around, although it is valid.
- I found references of DETACH and LOCK, how they work?
- Cascade DETACH causes the detach operation, when performed on the father, to also be performed on the son. We say that an entity is Detached when it is not being managed by Entitymanager. This happens when we run the Entitymanager detach(entity) method, when the transaction is committed or rollback.
- Cascade LOCK is not part of JPA but part of Hibernate. Basically when you acquire a lock in the parent entity, this operation will also be performed on the children.
This tutorial was helpful to me, it may help in your case. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
– Adário Muatelembe