Hi,
I was wondering if it is possible to tell Hibernate how to deal with cascade delete and inheritance. Well, let me explain the situation. I have implemented the classes and the according mapping for the following outcut of an ER diagram.
The entity type "AbstractResource" has the following attributes:
- ID
- Title
This ET is supposed to abstract from all the resources from which I don't have all the information I need to store it as a concrete "Resource" like "Book" or "Serial". Though there is a lack of information, I'd like to keep it in the database since it is possible to name such a resource as a referencing one for another concrete resource.
The relationship should express this circumstance. Concrete "Resources" can be referenced either by (other) concrete "Resources" or by "AbstractResource".
When it comes to deletion of a concrete resource I want to distinguish between referencing concrete resources and referencing abstract resources.
Let's consider the following situation:
Book A which is a concrete resource is referenced by AbstractResource X.
As soon as I delete Book A I want Hibernate to delete AbstractResourceX as well, because in my scenario it does not make sense to keep it in the database.
Whilst:
Book A is again a concrete resource and is referenced by Book B which is another concrete resource. In this case I don't want Hibernate to cascade the deletion to the child class.
Right now I can either delete all the referencing resources, no matter if it is an abstract or a concrete one, or just delete the resource that is supposed to be deleted anyway.
I hope the following excerpt of the mapping file is sufficient:
Code:
<class name="AbstractResource" table="abstractresource">
<id name="id" column="ResourceID" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="title" column="Title"/>
<set name="referencedBy" table="containedin" cascade="save-update" inverse="false">
<key column="ResourceID2"/>
<many-to-many column="ResourceID" class="AbstractResource"/>
</set>
<joined-subclass name="Resource" table="resource">
<set name="containedIn" table="containedin" cascade="save-update" inverse="false">
<key column="ResourceID"/>
<many-to-many column="ResourceID2" class="AbstractResource"/>
</set>
[...]
Is there any way to tell Hibernate to act in the way described above?
Lots of thanks in advance,
Florian