Skip to content

Document that JpaSpecificationExecutor.delete(…) uses CriteriaDelete and hence Cascades are not considered #2936

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

Closed
bilak opened this issue May 4, 2023 · 6 comments
Assignees
Labels
type: documentation A documentation update

Comments

@bilak
Copy link

bilak commented May 4, 2023

Hello,
When executing JpaSpecificationExecutor.delete(Specification<T> spec) the related entities which contains CascadeType.ALL are not deleted. However this works in CrudRepository.delete(T entity) or CrudRepository.deleteById(ID id).
Is this a bug (related to hibernate?) or is there any possibility to delete related entities with specification? I didn't found any info about this in documentation.

Sample app here with this test.

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

quaff commented May 5, 2023

I think it's a limitation of ORM, for performance concern, batch delete will not load them into memory then delete one by one.

@bilak
Copy link
Author

bilak commented May 5, 2023

@quaff I don't understand what you mean. As I wrote, it works in CrudRespository.delete so it's not an limitation of ORM.

@quaff
Copy link
Contributor

quaff commented May 5, 2023

CrudRepository.deleteById(ID id) will call EntityManager.find() then call EntityManager.remove() which will fire event notify cascading.
JpaSpecificationExecutor.delete(Specification<T> spec) is implemented by CriteriaDelete which will only issue one single DELETE statement, cascading is not aware.

You should do this if insist cascading

jpaSpecificationExecutor.findBy(spec, q -> q.stream()).forEach(jpaRepository::delete)

Please note it will be slow if result is large.

@bilak
Copy link
Author

bilak commented May 5, 2023

@quaff thanks for explanation. I think it's not very consistent then and might be documented somewhere at least. I see developers using both of those methods (crud vs specs) and it leads to confusion mainly when ORM generates restrict by JPA definition.

@quaff
Copy link
Contributor

quaff commented May 5, 2023

Criteria API bulk delete operations map directly to database delete operations. The persistence context is not synchronized with the result of the bulk delete.

https://docs.oracle.com/javaee/7/api/index.html?javax/persistence/criteria/CriteriaDelete.html

@christophstrobl christophstrobl added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels May 8, 2023
@mp911de mp911de changed the title JpaSpecificationExecutor.delete doesn't respect Cascade.ALL Document that JpaSpecificationExecutor.delete(…) uses CriteriaDelete and hence Cascades are not considered May 10, 2023
@mp911de mp911de changed the title Document that JpaSpecificationExecutor.delete(…) uses CriteriaDelete and hence Cascades are not considered Document that JpaSpecificationExecutor.delete(…) uses CriteriaDelete and hence Cascades are not considered May 10, 2023
@mp911de mp911de added this to the 3.0.6 (2022.0.6) milestone May 10, 2023
mp911de added a commit that referenced this issue May 10, 2023
…te` and hence `Cascades` are not considered.

Closes #2936
@mp911de
Copy link
Member

mp911de commented May 10, 2023

We updated our documentation to reflect that we're using CriteriaDelete when using specifications.

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

5 participants