views:

363

answers:

2

Hi,
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

A: 

Timestamps in Hibernate are apparently always updated automatically when the entity changes, so you can't use a <timestamp> mapping for the creation date. However, you can store it as a simple java.util.Date property, initialized it with new Date().

For the update timestamp, try this:

public class MyEntity {
  ...
  private Date updateDate;
  ...
}

<class name="MyEntity" table="MY_ENTITY">
  <id .../>
  <timestamp name="updateDate" access="field" column="UPDATE_DATE"/>
  ...
</class>

Note that timestamp must come right after id in the mapping.

FYI here is a reference of the timestamp attributes.

Péter Török
You can control this behavior with the `generated` attribute.
Pascal Thivent
@Pascal The reference linked above states: "`insert`: the given property value is generated on insert, but is not regenerated on subsequent updates. [...] Even though `version` and `timestamp` properties can be marked as `generated`, this option is not available."
Péter Török
Indeed, you're right. I went too fast. And now that I've re-read the doc carefully, I even wonder if one should use `timestamp` for "regular" fields like `createDate` or `updateDate` (my understanding is that you should use `timestamp` for `versioning` and this explains why `generated` doesn't make sense for `timestamp`), this seems to be an abuse of `timestamp`.
Pascal Thivent
@Pascal True, albeit a pretty common one (judging from the number of examples I found on the web). Which is sort of understandable as the other options listed in your answer all are more complicated, requiring triggers/interceptors/event listeners...
Péter Török
To be honest, I find that the documentation on `generated` sucks, it's unclear and misleading. (BTW, I didn't mean to harass you with my comment, I made a big typo that I wanted to fix, hence the delete/repost).
Pascal Thivent
@Pascal That's what I thought, I didn't perceive anything offending in what you did :-)
Péter Török
+2  A: 
Pascal Thivent
That's nice. But which type should i set for the createDate and updateDate properties...you code snippet ends before the type attribute :)
Marco
@Marco The `type` attribute is optional. If you use a `java.util.Date` at the Java level, there is no need to specify it.
Pascal Thivent
Thanks. Anyway this solution involves the creation of specific database triggers right?
Marco
@Marco For the updateDate, indeed (the createDate could just use a "default" value). I'll update my answer.
Pascal Thivent
+1 thorough investigation - I learnt a lot from it too!
Péter Török
@Péter Thanks. Glad you found it useful too.
Pascal Thivent