Set creation and update time with Hibernate in Xml mappings

Mark picture Mark · Apr 14, 2010 · Viewed 10.1k times · Source

I'm using Hibernate with Xml mappings. I have an entity that has two fields creationDate and updateDate of type timestamp, that have to be filled with the current UTC time when the entity is persisted and updated. I know about the existence of the @PrePersist and @PreUpdate annotations, but i don't know how to use their equivalent in my Xml mappings.

Again, i was wondering if Hibernate somehow supports natively the update and creation time set.

Thanks

Answer

Pascal Thivent picture Pascal Thivent · Apr 14, 2010

I know about the existence of the @PrePersist and @PreUpdate annotations, but i don't know how to use their equivalent in my Xml mappings.

The Hibernate3 event architecture provides something equivalent and you could register listeners for PreInsertEvent, PreUpdateEvent or SaveOrUpdateEvent (see the org.hibernate.event package for a full list) to set and update the create/update dates.

Another approach would be to use an interceptor, either Session-scoped or SessionFactory-scoped and to set both createDate and updateDate in onSave(...), update the updateDate in onFlushDirty(...).


Update: I'm leaving my original suggestions below but I think that the right approach (should have been my initial answer) is to use an interceptor or the event architecture.

You could use the generated attribute of the timestamp to get creationDate and updateDate generated by the database on insert and on insert and update respectively:

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="createDate" generated="insert" ... />
  <timestamp name="updateDate" generated="always" ... />
  ...
</class>

Refer to the section on generated properties for full details.

Option 1

It appears that timestamp doesn't support generatead so my suggestion won't work. Nevertheless, having read the documentation more carefully, my understanding is that timestamp is an alternative to versioning and I don't think that it's an appropriate choice for fields like createDate and updateDate (it may work for the later but that's not what timestamp is for).

So I would actually still use generated properties but with simple properties instead of timestamp:

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  <property name="updateDate" update="false" insert="false" generated="always" ... />
  ...
</class>

At the database level, this would require using a trigger for updateDate column. For the createDate column, using something like current_timestamp as default value would work nicely. But triggers are maybe not wanted...

Option 2

To avoid the trigger of the Option 1, a variation would be to use updateDate for versioning (and thus map it as timestamp):

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" ... />
  <property name="createDate" update="false" insert="false" generated="insert" ... />
  ...
</class>

Same approach as Option 1 for createDate, use a default value at the database level.

Option 3

See the top of this answer...