Closed
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
quaff commentedon 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 commentedon 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 commentedon May 5, 2023
CrudRepository.deleteById(ID id)
will callEntityManager.find()
then callEntityManager.remove()
which will fire event notify cascading.JpaSpecificationExecutor.delete(Specification<T> spec)
is implemented byCriteriaDelete
which will only issue one single DELETE statement, cascading is not aware.You should do this if insist cascading
Please note it will be slow if result is large.
bilak commentedon 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 commentedon May 5, 2023
https://docs.oracle.com/javaee/7/api/index.html?javax/persistence/criteria/CriteriaDelete.html
[-]JpaSpecificationExecutor.delete doesn't respect Cascade.ALL[/-][+]Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDelete` and hence `Cascades` are not considered [/+][-]Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDelete` and hence `Cascades` are not considered [/-][+]Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDelete` and hence `Cascades` are not considered[/+]Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDele…
mp911de commentedon May 10, 2023
We updated our documentation to reflect that we're using
CriteriaDelete
when using specifications.Document that `JpaSpecificationExecutor.delete(…)` uses `CriteriaDele…