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

Unable to use deleteInBatch due to cascade issue #3198

Closed
BalachandranM opened this issue Oct 14, 2023 · 3 comments
Closed

Unable to use deleteInBatch due to cascade issue #3198

BalachandranM opened this issue Oct 14, 2023 · 3 comments
Assignees
Labels
type: documentation A documentation update

Comments

@BalachandranM
Copy link

I have entity A and B.

A is binded to B in a one-to-one relationship while the owning side is B in the relation(i.e, a_id is in B).

The issue that I faced is, when I try to delete a collection of A objects using repo.delete method, respective elements of B is deleted and then A is deleted, which is fine. But, when I try to delete the same collection of A objects using repo.deleteInBatch method, elements of B are not pushed for delete and the ORM is directly trying to delete the records of A because of which I get into exceptions.. It appears to be an issue with the cascade.

This seems to be a bug with deleteInBatch method since the same is working for regular deletes.

I am using Spring Boot 2.7.3
JDK: 1.8
DB: Postgresql

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 14, 2023
@quaff
Copy link
Contributor

quaff commented Oct 16, 2023

No, It's not a bug, it's used for bulk delete with one single query. Here is the Javadoc of deleteInBatch():

Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs first level cache and the database out of sync.

I think the javadoc need be improved to clarify that cascade and events are ignored in this case, You should consider using deleteAll() instead of deleteAllInBatch(), the former supports cascade and events and will delete one by one instead of delete with one single query, same relation apply to deleteAllById() and deleteAllByIdInBatch().

It's contrary to #3177

@mp911de mp911de assigned gregturn and schauder and unassigned gregturn Oct 16, 2023
@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 16, 2023
@schauder
Copy link
Contributor

@quaff is right on the spot, including that the documentation could use some improvement, which I did with 685107d

schauder added a commit that referenced this issue Oct 17, 2023
schauder added a commit that referenced this issue Oct 17, 2023
@BalachandranM
Copy link
Author

Thanks guys for the quick response 👍 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

6 participants