Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for delete parent entity with @OneToMany cascade = ALL #69

Closed
wants to merge 3 commits into from
Closed

Add tests for delete parent entity with @OneToMany cascade = ALL #69

wants to merge 3 commits into from

Conversation

ivan-zaitsev
Copy link
Contributor

@ivan-zaitsev ivan-zaitsev commented Nov 9, 2021

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 Group
OR
@OnDelete(action = OnDeleteAction.CASCADE) instead of cascade = CascadeType.ALL
OR
entityManager.clear(); before delete Audience
OR
add sql cascades and @GeneratedValue(strategy = GenerationType.SEQUENCE)


Just for examination, spring data jpa issue: spring-projects/spring-data-jpa#2281

@vladmihalcea
Copy link
Owner

Thanks. I'll check it out when I have some time.

Ivan Zaitsev added 2 commits November 10, 2021 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants