Add tests for delete parent entity with @OneToMany cascade = ALL #69
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.
Hi, Vlad. I wrote some tests with delete operation after tricky select.
Test class -
DeletingAfterFetchWithoutSQLCascadeTest
Test method -
testDeleteParentAfterEagerInnerFetchInChildWithoutSQLCascadeInOneTransaction
When we try to delete parent entity (Audience) after select with join fetch with list of Lesson and inside them also join list of Group (groups also have lessons, and they have EAGER fetch mapping). The
ConstraintViolationException
will occur after parent (Audience) delete.Even if we try to delete child entities (Lesson) explicitly before parent delete (Audience), anyway the same exception will occur.
Test method -
testDeleteParentAfterEagerInnerFetchInChildWithoutSQLCascadeThenSaveAnotherEntityInDifferentTransactions
In this test case we have the same logic, but do all operations in different transactions.
Test class -
DeletingAfterFetchWithSQLCascadeTest
Test method -
testDeleteParentAfterEagerInnerFetchInChildWithSQLCascadeInOneTransaction
In this test case we have similar logic, but the database lesson table doesn't have
ON DELETE CASCADE
constraint. And we have another exception -TransientPropertyValueException
.Test method -
testDeleteParentAfterEagerInnerFetchInChildWithSQLCascadeThenSaveAnotherEntityInDifferentTransactions
In this test case we have the same logic, but if we do all operations in different transactions.
And the
TransientPropertyValueException
will occur not after parent (Audience) delete, but in the next operation, for example - merge different Audience that doesn't belong to the main select with join fetch.Possible solutions to resolve this problem:
set
fetch = FetchType.LAZY
for lessons in GroupOR
@OnDelete(action = OnDeleteAction.CASCADE)
instead ofcascade = CascadeType.ALL
OR
entityManager.clear();
before delete AudienceOR
add sql cascades and
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Just for examination, spring data jpa issue: spring-projects/spring-data-jpa#2281