I'm curious and need to find this answer quick. Google won't help much.
The Java Persistence API has these properties that tell the framework to cascade operations on associated entities:
CascadeType.PERSIST CascadeType.DELETE CascadeType.MERGE CascadeType.REFRESH
I know what the first two mean: when I persist object A which has B, persist B as well, and when I delete A, delete B as well.
But I can't make any sense of what the other two accomplish. Help?
CascadeType. MERGE : cascade type merge means that related entities are merged when the owning entity is merged.
Persist should be called only on new entities, while merge is meant to reattach detached entities. If you're using the assigned generator, using merge instead of persist can cause a redundant SQL statement.
The EntityManager. refresh() operation is used to refresh an object's state from the database. This will revert any non-flushed changes made in the current transaction to the object, and refresh its state to what is currently defined on the database.
At a very high level, refresh() means pulling any state changes from the database that have been done outside the current Session and after the entity has been loaded. Cascading the refresh() means that all associated entities are also refreshed.
JPA Annotation Meaning for Many to Many relationships:
I myself see them this way (more readable):
REFRESH means "pull any state changes from the database into my representation". Cascading this is simple; it means that all associated entities are refreshed.
MERGE means something complex that approximates "save" but is more like "push this detached entity back into managed status and save its state changes"; the cascading means that all associated entities get pushed back the same way, and the managed-entity handle you get back from .merge()
has all managed entities associated with it.
Link to one instance of the relevant docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With