Allows say I've two tables - "child" and "parent" with lots of-to-one relation. Things I require is to remove child records if parent record is erased.

It's not an issue basically link child table from parent by creating one-to-many association in parent.hbm and hang cascade="all-remove-orphan".

The issue is I'm not going one-to-many relation around the parent side, and so i produced many-to-one around the child side. The main reason for your is child table is fairly large and I'd rather not extract 100s of records each time I personally use parent. So my configuration appears like this:

child.hbm:

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

while parent.hbm doesn't have associations with child.

Now you ask ,: Steps to make Hibernate remove records from child table when removing a parent or gaurdian if your child is related to some parent with lots of-to-one?

Thanks.

Handful of options:

  • add the main one-to-many towards the parent with cascading down remove, but mitigate the performance loss using lazy loading.

  • make use of a Hibernate Interceptor (or perhaps an take into account an AOP atmosphere) to identify parent record deletions and remove children.

Personally I'd favour the very first option, because it allows your computer data model more carefully reflect the actual associations inside your data.

Edit: there is a third option, but it is not enjoyable - make use of a database trigger, and flush your Hibernate cache (or make use of a non-caching session).