What are the types of Scade in JPA?

Asked

Viewed 18,212 times

11

I’m looking for information on the types of Scade that exist and how they work in JPA relational modeling. The guys I found explanations for were:

  • NONE = Does nothing with the object (default)

  • MERGE = Update children when updating the father, only if you are already searching

  • PERSISTS = Save your child when you save your father

  • REFRESH = Save the father and keep the son unchanged

  • REMOVE = Remove the child when removing the parent and vice versa

  • ALL = Performs all operations of Cache

I have the following doubts:

  1. The explanation of how these types of cascade?

  2. Mapping with mappedBy should be placed the cascade only on the parent object?

  3. I found references to cascade DETACH and LOCK, how they work?

  • 1

    This tutorial was helpful to me, it may help in your case. https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/

1 answer

10


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.

  1. 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.
  1. 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.
  1. 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.