Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I know about the existence of the <code>@PrePersist</code> and <code>@PreUpdate</code> annotations, but i don't know how to use their equivalent in my Xml mappings.</p> </blockquote> <p>The Hibernate3 <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/events.html#objectstate-events" rel="noreferrer">event architecture</a> provides something equivalent and you could register listeners for <code>PreInsertEvent</code>, <code>PreUpdateEvent</code> or <code>SaveOrUpdateEvent</code> (see the <code>org.hibernate.event</code> package for a full list) to set and update the create/update dates.</p> <p>Another approach would be to use an <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/events.html#objectstate-interceptors" rel="noreferrer">interceptor</a>, either <code>Session</code>-scoped or <code>SessionFactory</code>-scoped and to set both <code>createDate</code> and <code>updateDate</code> in <code>onSave(...)</code>, update the <code>updateDate</code> in <code>onFlushDirty(...)</code>.</p> <hr> <p><strong>Update:</strong> 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.</p> <p><strike>You could use the <strong><code>generated</code></strong> attribute of the <strong><a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-timestamp" rel="noreferrer"><code>timestamp</code></a></strong> to get <strong><code>creationDate</code></strong> and <strong><code>updateDate</code></strong> generated by the database on insert and on insert and update respectively:</p> <pre><code>&lt;class name="MyEntity" table="MY_ENTITY"&gt; &lt;id .../&gt; &lt;timestamp name="createDate" generated="insert" ... /&gt; &lt;timestamp name="updateDate" generated="always" ... /&gt; ... &lt;/class&gt; </code></pre> <p>Refer to the section on <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-generated" rel="noreferrer">generated properties</a> for full details.</strike></p> <h3>Option 1</h3> <p>It appears that <strong><code>timestamp</code></strong> doesn't support <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-generated" rel="noreferrer"><strong><code>generatead</code></strong></a> so my suggestion won't work. Nevertheless, having read the documentation more carefully, my understanding is that <strong><code>timestamp</code></strong> is an alternative to versioning and I don't think that it's an appropriate choice for fields like <strong><code>createDate</code></strong> and <strong><code>updateDate</code></strong> (it may work for the later but that's not what <code>timestamp</code> is for). </p> <p>So I would actually still use <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-generated" rel="noreferrer">generated properties</a> <strong>but</strong> with <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-property" rel="noreferrer">simple properties</a> instead of <strong><code>timestamp</code></strong>:</p> <pre><code>&lt;class name="MyEntity" table="MY_ENTITY"&gt; &lt;id .../&gt; &lt;property name="createDate" update="false" insert="false" generated="insert" ... /&gt; &lt;property name="updateDate" update="false" insert="false" generated="always" ... /&gt; ... &lt;/class&gt; </code></pre> <p>At the database level, this would require using a trigger for <code>updateDate</code> column. For the <code>createDate</code> column, using something like <code>current_timestamp</code> as default value would work nicely. But triggers are maybe not wanted...</p> <h3>Option 2</h3> <p>To avoid the trigger of the <strong>Option 1</strong>, a variation would be to use <code>updateDate</code> for versioning (and thus map it as <code>timestamp</code>):</p> <pre><code>&lt;class name="MyEntity" table="MY_ENTITY"&gt; &lt;id .../&gt; &lt;timestamp name="updateDate" ... /&gt; &lt;property name="createDate" update="false" insert="false" generated="insert" ... /&gt; ... &lt;/class&gt; </code></pre> <p>Same approach as Option 1 for <code>createDate</code>, use a default value at the database level.</p> <h3>Option 3</h3> <p>See the top of this answer...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload