Panache: deleteAll not deleting child entities when using cascade ALL or REMOVE
See original GitHub issueDescribe the bug When calling the deleteAll function to delete all the entities. It’s expected to delete also the child entities if it has been configured to use cascade ALL or REMOVE:
@Entity(name = "application")
public class ApplicationEntity extends PanacheEntity {
// ...
@OneToMany(mappedBy = "application", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
public List<ServiceEntity> services = new ArrayList<>();
Delete all method usage:
ApplicationEntity.deleteAll();
This works fine if we delete the entity individually:
ApplicationEntity.findById(actualEntityId).delete();
Expected behavior Delete one item and delete all should behave the same, so both methods should delete the child entities if cascade is properly configured.
Actual behavior DeleteAll method is not deleting child entities even though the cascade is properly configured.
To Reproduce You can run this test as a reproducer. The test is failing and it should pass.
Environment (please complete the following information):
- GraalVM version (if different from Java):
- Quarkus version or git rev: 1.10.3.Final
- Build tool (ie. output of
mvnw --version
orgradlew --version
):
Issue Analytics
- State:
- Created 3 years ago
- Comments:35 (19 by maintainers)
Top Results From Across the Web
JPA OneToMany not deleting child - java - Stack Overflow
Here cascade, in the context of remove, means that the children are removed if you remove the parent. Not the association. If you...
Read more >Why you should avoid CascadeType.REMOVE for to-many ...
The CascadeTypes REMOVE and ALL, which includes REMOVE, provide a comfortable option to remove an entity together with all its child entities.
Read more >deleteById not actually deleting, while custom @Modifying ...
I have an entity called TermConcept, which has a whack of child entities ... I have code which is attempting to delete these,...
Read more >The best way to soft delete with Hibernate - Vlad Mihalcea
Therefore, we will discuss the mapping of all these entities as well ... there will be no duplicate delete clause, but then the...
Read more >How to Delete Child Records in OneToMany Relationship ...
Can Hibernate automatically delete the child entity if I remove its ... @OneToMany(mappedBy = "vendor", cascade = CascadeType.ALL)
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well we can start with improving the javadoc to inform that it doesn’t do cascade, that’s an easy enough PR 😃
We can also add this to the documentation and suggest the alternative code I showed above. Again an easy PR 😃
In the end, does it matter if the code is well-written and performant if it doesn’t work? 😉 BTW, I have no experience with Spring whatsoever. So, I’m not biased because of it.
I agree. In the absence of a better solution, maybe removing the
deleteAll
method is the way to go. It could have saved me a few hours of debugging.As a matter of fact, a colleague reported the same issue yesterday. And when this kind of error happens, our first thought is: “what have I done wrong in my entity configuration”? So, you spend a few hours until you realize the
deleteAll
doesn’t cascade. We had that lesson learned. How many developers will have to go through the same experience?Not crazy at all. I can’t recall having to recur to the
deleleAll
method in production code from my experience. At least not on purpose. 😅 I’ve resorted to it in test cases multiples times, though, especially considering Quarkus applications testing nature.Sounds like a plan.
Thank you, @gavinking and @FroMage, for taking the time to look into this matter. I’d be glad to provide a pull request regardless of the chosen solution. I just need some guidance. 😀