Hello,
The problem is the following:
If i want my mapping to make hibernate handle the list-index, i have to remove inverse="true" and on-delete="cascade" in <list name="hourEntries">
But, if I remove inverse="true" and on-delete="cascade", hibernate doesn't apply an on delete cascade on the foreign key.
Another guy with the same problem hacks in the hibernate source and changes Mappings.Collection.validate() ->
http://lists.jboss.org/pipermail/hibern ... 08224.html
but that doesn't seem to work for me and is a dirty solution.
Hibernate:
Hibernate 3.3.1.GA
Dialect:
org.hibernate.dialect.MySQL5InnoDBDialect
MySQL:
5.0.22
I have the following mappings:
RegistrationEntry (Parent)
Code:
<hibernate-mapping>
<class name="nl.ncim.timetracking.model.RegistrationEntry" table="registration_entry">
<id name="registrationEntryID" column="registrationentry_id" unsaved-value="0">
<generator class="native"/>
</id>
...
<property name="registrationEntryOrder" column="registrationentry_order" type="int" update="true" insert="true" not-null="true"/>
...
<list name="hourEntries" access="field" cascade="all,delete-orphan" lazy="false">
<key column="registration_entry" on-delete="cascade"/>
<index column="hourentry_order"/>
<one-to-many class="nl.ncim.timetracking.model.HourEntry" />
</list>
</class>
</hibernate-mapping>
HourEntry (Child)
Code:
<hibernate-mapping>
<class name="nl.ncim.timetracking.model.HourEntry" table="hour_entry">
<id name="hourEntryID" column="hourentry_id" unsaved-value="0">
<generator class="native"/>
</id>
....
<property name="hourEntryOrder" column="hourentry_order" type="int" update="false" not-null="true"/>
<many-to-one name="registrationEntry" class="nl.ncim.timetracking.model.RegistrationEntry"
column="registration_entry" foreign-key="RegistrationEntryFK" not-null="true" />
</class>
</hibernate-mapping>
If I add on-delete: only inverse one-to-many associations may use on-delete="cascade"
Without on-delete: Cannot delete or update a parent row: a foreign key constraint fails
If i add inverse: IndexOutOfBoundsException when trying to fetch the list
I guess its unneeded, but in the case it helps to clarify, the getter and setter for the hourEntryOrder:
Code:
public int getHourEntryOrder() {
RegistrationEntry regEntry = this.getRegistrationEntry();
if(regEntry != null && regEntry.getHourEntries() != null) {
return regEntry.getHourEntries().indexOf(this);
} else {
return 0;
}
}
public void setHourEntryOrder(int hourEntryOrder) {
//this.hourEntryOrder = hourEntryOrder;
}
Thanks in advance for any help,
Joost Pastoor