Skip to content

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

Closed
@bilak

Description

@bilak

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.

Activity

quaff

quaff commented on May 5, 2023

@quaff
Contributor

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

bilak commented on May 5, 2023

@bilak
Author

@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

quaff commented on May 5, 2023

@quaff
Contributor

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

bilak commented on May 5, 2023

@bilak
Author

@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

quaff commented on May 5, 2023

@quaff
Contributor

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

changed the title [-]JpaSpecificationExecutor.delete doesn't respect Cascade.ALL[/-] [+]Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDelete` and hence `Cascades` are not considered [/+] on May 10, 2023
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[/+] on May 10, 2023
added this to the 3.0.6 (2022.0.6) milestone on May 10, 2023
added a commit that references this issue on May 10, 2023
b9bd0e7
mp911de

mp911de commented on May 10, 2023

@mp911de
Member

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

added a commit that references this issue on Feb 11, 2024
d2fa85a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @quaff@mp911de@christophstrobl@bilak@spring-projects-issues

      Issue actions

        Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDelete` and hence `Cascades` are not considered · Issue #2936 · spring-projects/spring-data-jpa