Note that there are some explanatory texts on larger screens.

plurals
  1. POEclipseLink JPA `@PreUpdate` call not persisting
    primarykey
    data
    text
    <p>I ran into some similar questions on StackOverflow, tried the solutions, but did not find an answer. </p> <p>I am using a fairly common JPA strategy to set last modified times on some entities. Set up the columns and the fields, then tag a method with <code>@PreUpdate</code> and let it set them equal to the current time.</p> <p>The problem is that I can see in the debugger that the method is being called and that the field is being updated, however in my DB logs I only see a SQL call to UPDATE the changed field that does NOT include an UPDATE for the timestamp field.</p> <p>Complicating things further <code>@PrePersist</code> works perfectly, only <code>@PreUpdate</code> exibits this behaviour.</p> <p>The closest explanation I've found so far is at this <a href="http://www.eclipse.org/forums/index.php?t=msg&amp;goto=499756&amp;S=3bcc64b0e0822e7707f2ce0b616d92df" rel="nofollow noreferrer">LINK</a>.</p> <p>Similar questions at: #1725699 and #1745890</p> <p>I am using EclipseLink v2 and JPA v1 for compatibility with GlassFish v2.</p> <p>I have attempted using both annotations directly on methods in the Entity class as well as an <code>EntityListener</code> attached to the Entity class with the <code>@EntityListener</code> annotation.</p> <p>I suspect this is a bug in EclipseLink, but I can't prove it. </p> <p>Bug or not I would very much like this simple operation to work. Is there anything wrong with this implementation? Is this a known issue in EclipseLink? Is this a known issue in JPA? Is there a way around this? </p> <p>Short of going to the database and using triggers, is there an alternate path to let my Java code set the updated_on timestamp?</p> <p>Thanks for the advice!</p> <p>Code snippets follow.</p> <p>Entity fields:</p> <pre><code>@Column(name = "updated_on") @Temporal(TemporalType.TIMESTAMP) private Date updatedOn; @Column(name = "created_on") @Temporal(TemporalType.TIMESTAMP) private Date createdOn; </code></pre> <p>Annotated update methods:</p> <pre><code>@PreUpdate public void setUpdatedOn(Timestamped object) { object.setUpdatedOn(new Date()); } @PrePersist public void setCreatedOn(Timestamped object) { if (object.getCreatedOn()==null) { object.setCreatedOn(new Date()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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