We have a problem with the usage of joined-subclass when changing between the superclass and subclass for persistent entities. We use the following mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="whatever.domain">
<class name="Sensor" table="SENSOR" lazy="true">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">SENSOR_SEQ</param>
</generator>
</id>
<property name="date" type="date" column="DATE"/>
<joined-subclass name="CamSensor" table="CAM_SENSOR" lazy="true">
<key column="ID"/>
<many-to-one name="format" column="FORMAT" not-null="true" class="PicFormatType" lazy="false"/>
<property name="infrared" type="character" column="INFRARED"/>
</joined-subclass>
</class>
</hibernate-mapping>
A camera sensor (camsensor) is a sensor with some more attributes and relations. We would like to be able to change a persistent sensor into camera sensor, and vice versa. We do the former by retrieving a sensor object from the database, copying all information contained in the object into a camera sensor object, and try to save the new camera sensor to the database. This does however not work, and we get the following exception.
Code:
Could not flush session:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [whatever.domain.CamSensor#21263]
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [whatever.domain.CamSensor#21263]
When we want to change the type of a camera sensor into a regular sensor, we do it the same way. We retrieve the camera sensor from the database, copy all relevant values into a new sensor object, and save the new sensor back into the database. This does not give us any exception, but behaves unlike we expect. The information in our sensor-table is updated, but the entry in the cam_sensor-table is not altered, so when Hibernate retrieves the object again later, it appears to be a camera sensor.
Are we doing something the wrong way, or have we missed an important bit? Suggestions would be appreciated!