Add cascade removal for known entities #2031
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Introduces cascade removal strategies, which are:
orphanRemoval
or@CascadeOnDelete
those strategies require entries to declareeither bidirectional mapping, or mapping present on owning side, for example
Profile
depends onUser
whileUser
doesn't know anything about profile, so for removing profile along with a user we have to declare Profile mapping on User object side, and mark the dependency withorphanRemoval
, this is not always possible as objects may be in separate modules and it will produce circular dependencies.foreignKeyDefinition
for defining foreign keys e.g.:While this strategy doesn't require to define dependenies on owning side, it is RDBMS specific,
another problem is that it won't affect Caches(L2 is a problem here) as all the removal stuff will be performed by the database.
EventService
for removing entities.As
EventService
is synchronous we can publish events and wait while all the notifiers proceed those events. On the other hand we don't use any native queries and always remove entries usingEntityManager#remove
method, which means that JPA takes care of removal and publishes entity related events. In that case we can connect JPA events andEventService
events to allow another modules/entities/components to react on the removal. All the database interaction methods are marked with the@Transactional
annotation which may be nested.The example:
If any entry removal fails, then transaction will be rolled back and all the data will be in consistent state.
Another nice thing in this strategy is that we can create application level components which may react on such kind of events and rollback transactions on the application level(which is useful for recipes and stacks their removal will be based on permissions).
It is still possible and preferable to use
orphanRemoval
for those entities which are not root model entities, for instanceWorkspace
relies on theWorkspaceConfig
withorphanRemoval
attribute.I implemented the last strategy as it is the most appropriate. Along with that i changed
current Local dao implementations to use self monitor for locking, as it allows to extend them with a new functionality which may also be thread-safe as client becomes allowed to use instance locks, this may be needed for migration tools.
What issues does this PR fix or reference?
#1803
Previous Behavior
No cascade removals
New Behavior
All the entries removed when any of entries which they depend on is removed.
Tests written?
Yes
@skabashnyuk, @sleshchenko, @akorneta please review