Hello
I am using hibernate 3.0 and have problem with one-to-one polymorphic property uptade.
I am working with detached objects.
Code:
<hibernate-mapping>
<class name="com.kovine.kfe.dao.TableColumn" table="KFE_TABLE_COLUMN">
<id name="id" unsaved-value="null" column="TABLE_COLUMN_ID">
<generator class="sequence">
<param name="sequence">KFE_TABLE_COLUMN_ID_SEQ</param>
</generator>
</id>
...
<one-to-one name="inputType" class="com.kovine.kfe.dao.InputType" cascade="all" />
</class>
<class name="com.kovine.kfe.dao.InputType" abstract="true">
<id name="id" column="TABLE_COLUMN_ID">
<generator class="foreign">
<param name="property">tableColumn</param>
</generator>
</id>
<one-to-one name="tableColumn" class="com.kovine.kfe.dao.TableColumn" constrained="true" />
<union-subclass name="com.kovine.kfe.dao.InputTypeText" table="KFE_INPTYPE_INPUTTEXT">
<property name="width" column="WIDTH"></property>
</union-subclass>
<union-subclass name="com.kovine.kfe.dao.InputTypeTextarea" table="KFE_INPTYPE_INPUTTEXTAREA">
<property name="width" column="WIDTH" not-null="true" />
<property name="height" column="HEIGHT" not-null="true" />
</union-subclass>
</class>
</hibernate-mapping>
Code:
InputType inputType = new TextareaInputType();
... set inputType properties ...
// update
Transaction tx = session.beginTransaction();
InputType old = tableColumn.getInputType();
tableColumn.setInputType(null);
session.delete(old);
inputType.setTableColumn(tableColumn);
tableColumn.setInputType(inputType);
session.update(tableColumn);
tx.commit();
This code throws exception:
Code:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.kovine.kfe.dao.InputTypeText#251]
But I think this message is wrong, because there is a new object inputType which should replace old one. Maybe this message was caused because hibernate only compare ids of related object and if they are the same it consider those object as same too. If I am right, then it could be a bug in hibernate.
Using this fragment of code:
Code:
InputType inputType = new TextareaInputType();
... set inputType properties ...
// update
Transaction tx = session.beginTransaction();
inputType.setTableColumn(tableColumn);
tableColumn.setInputType(inputType);
session.update(tableColumn);
tx.commit();
causes duplicate entry in inputType tables (the old entry doesn't delete).
Please help me to solve this problem. I was trying many other solutions but none has worked.
Thanks in advance!
Martin