question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Panache: deleteAll not deleting child entities when using cascade ALL or REMOVE

See original GitHub issue

Describe 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 or gradlew --version):

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:35 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
FroMagecommented, Dec 18, 2020

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 😃

1reaction
hprangecommented, Dec 18, 2020

We’re surprised by things we’re not used to: you’re used to Spring, and I’m used to data access code that isn’t crap 🤷‍♂️😆

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.

Sometimes it’s better to simply not have a feature than to have a bad implementation of the feature.

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?

a crazy thought that just occurred to me is that the only actual usecase we have for this thing is cleaning up after tests.

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.

Perhaps all we need is a DB.deleteAll() then?

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. 😀

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found