I have a legacy database and each table has create_ts, update_ts columns populated via triggers. I ran into trouble with hibernate when wanted to update some rows and persistence manager treated detached object as transient, as result Hibernate inserted another row when I would have expected an update.
Below is an example of the mapping file and please note that I have changed the <Sql-update> generated command. Any thoughts?
Thanks,
valy
<hibernate-mapping>
<class name="org.ifm.model.Person"
table="Persons"
dynamic-insert="true"
dynamic-update="false"
select-before-update="true">
<id name="indi" type ="long"
column="INDI"
unsaved-value="0">
<generator class="sequence">
<param name="sequence">indi_seq</param>
</generator>
</id>
<timestamp
column="UPDATE_TS"
name="UpdateTs"
access="property"
source="db"
generated="always"
unsaved-value="undefined"/>
<property
name="firstName"
type="java.lang.String"
column="FIRST_NAME"
not-null="true"
length="30"/>
<property
name="lastName"
type="java.lang.String"
column="LAST_NAME"
not-null="true"
length="30"/>
<sql-update>
UPDATE persons
SET first_name=?,
last_name=?
WHERE indi=?
AND (indi_update_ts IS NULL OR indi_update_ts=?)
</sql-update>
</class>
</hibernate-mapping>
|