Hibernate: How do I get Hibernate to delete records from a child table when a parent is deleted if the child is associated with a plural parent?

Suppose I have two tables - “child” and “parent” with many relationships. I need to delete child records if the parent record is deleted.

This is not a problem if I associate the child table with the parent, creating a one-to-many association in parent.hbm and setting cascade = "all-delete-orphan".

The problem is that I don't want a one-to-many relationship on the parent side, so I created many-one on the child side. The reason for this is that the children’s table is quite large, and I don’t want to retrieve hundreds of records every time I use parent. Therefore, my configuration is as follows:

child.hbm:

<many-to-one name="parent" class="com.example.Parent" column="parentid"/>

while parent.hbm has no associations with a child.

Question: How do I get Hibernate to delete records from a child table when a parent is deleted if the child element is associated with a many-to-one parent?

Thanks.

+5
source share
1 answer

A couple of options:

, .

: , - - Hibernate ( ).

+5

All Articles