Hi -
I have a database table with a create date and a update date. For any update statement to the table, i want to update the UPDATE_TSP field with the CURRENT TIMESTAMP from the database.
This table is a 1-to-1 with a parent table, so the id is generated by the parent table.
I tried using <timestamp> tag, but it would always try to do an insert for saveOrUpdate calls when it should be updating, and thus toss a duplicate key exception.
The work around below using <sql-update> works, but seems to me not a good design since changing the order or modifying the properties for the fields will make the custom SQL fail.
Is there a better way to accomplish this?
Thanks
Hibernate version:
3.2.3.ga
DB2 8.2
Mapping documents:
<class name="Stuff" table="STUFF">
<id name="pid" column="P_ID" type="long">
<generator class="assigned" />
</id>
<property name="name" column="NAME" type="string" />
<property name="number" column="NUMBER" type="long" />
<property name="weight" column="WEIGHT" type="double" />
<property name="createDate" column="CREATE_TSP" type="java.util.Date" generated="insert" update="false" />
<property name="updateDate" column="UPDATE_TSP" type="java.util.Date" generated="always" />
<sql-update>update STUFF
set NAME = ?,
NUMBER = ?,
WEIGHT = ?,
UPDATE_TSP = current timestamp,
where P_ID = ?
</sql-update>
</class>
|