Hibernate: How to remove an entity to which none r

2019-09-09 06:36发布

问题:

I have two entities, lets call them A and B, which have a ManyToOne mapping to another entity, say C

So I typically have something like this:

a1->c

a2->c

b1->c

Lots of A's and B's pointing to the same C.

How do I get Hibernate to remove c when I remove the last a and b?

回答1:

It seems that JPA/Hibernate does not support this. Which is really bad.



回答2:

There is an attribute delete-orphan that you can use when mapping the many-to-one relation, this should do the work for you.

for more info. I advise visiting they documentation online about the cascading operations.