Hi all,
I have a question about how to map a situation where I have two sets (one-to-many relations) from one class to another.
I have a class Test to which Remarks can be attached. Remarks can be produced either by the user or by the system itself. I want to keep the Remarks separated since they are handled in different ways in the business logic. Simplest way to achieve this I think is to have two Sets of Remarks in my Test object.
Now the problem rises since according to the documentation "An instance of the contained entity class may not belong to more than one instance of the collection" when using one-to-many mappings.
So after reading that I tried to create a link table between the classes using many-to-many relation and I almost succeeded with these mappings int Test.hbm.xml:
Code:
<!-- User Remarks -->
<set name="userRemarks" sort="natural" table="test_user_remark_link" cascade="all">
<key column="test_id"/>
<many-to-many class="HibernateRemark" column="remark_id"/>
</set>
<!-- System Remarks -->
<set name="systemRemarks" sort="natural" table="test_system_remark_link" cascade="all">
<key column="test_id"/>
<many-to-many class="HibernateRemark" column="remark_id"/>
</set>
The problem with previous mappings is the cascading. When I remove Remarks from the Sets the orphan instances are left to the DB. I tried to change the cascading to "all-delete-orphan" and that leads to me getting exceptions about failing foreign key constants.
What would be the preferred solution to map this kind of situation with Hibernate?
Sincerely,
Jouni Hartikainen