I really love using Hibernate but sometimes I just get so stuck. I'm using a table-per-concrete class type structure and have a class that looks like this:
Code:
<class name="Subcomp" table="cab_subcomp">
<id name="id" column="_id" type="integer">
<generator class="identity" />
</id>
<property name="quantity" type="integer" column="_quantity" />
<many-to-one name="comp" class="Comp" column="_comp" />
<many-to-one name="category" class="Category" column="_category" fetch="join" not-found="ignore" cascade="none" />
</class>
My Comp table is the parent.
My Subcomp table is the child.
My Category table is an occassional relationship between some Subcomp records and a Category record. It certianly doesn't happen on every record.
As you can see I have tried defining fetch="join", not-found="ignore" and cascade="none" in the hope that one of these might signify an optional join. However it doesn't work - Hibernate still forces a foreign key constraint from column _category to the _id column of the Category table.
There is nothing defined in the Category table that makes any reference to the Subcomp table, and as such this is only a unidirectional link.
I have searched through endless documentation, forums and websites but found no way of creating an optional relation.
I can only see two options open to me:
1. Is to manage the relationship in my code, that is simply define a property 'category_id' and in my Subcomp table and manually retrieve the Category record when required.
2. Is to manually drop the foreign key constraint - the application works normally following this, however it then becomes an additional step required during installation - and it is certainly not an elegant solution.
Is there a better (more elegant) solution???
Cheers
Murray