| Problems with @PreUpdate and flush() [message #498894] |
Wed, 18 November 2009 23:37  |
Thomas Messages: 8 Registered: November 2009 |
Junior Member |
|
|
Hello all,
I'm trying to add auditing functionality to my entities, using an embedded entity and EntityListeners for updating the audit fields on prePersist and preUpdate.
It works except when an entity is updated and a flush is performed on the EntityManager:
- The @PreUpdate listener is invoked and changes the values
- But DatabaseQueryMechanism.updateObjectForWriteWithChangeSet() does ignore any @PreUpdate changes if it's executed within a EntityManager.flush().
Has anybody had similar problems? Is there a workaround? I'm trying to avoid committing and re-opening a new transaction.
I'm experiencing this problem in EclipseLink v1.1.2, 1.1.3 and 1.2.0.
Thanks
Thomas
|
|
|
|
|
|
| Re: Problems with @PreUpdate and flush() [message #499690 is a reply to message #498894] |
Mon, 23 November 2009 13:05   |
James Sutherland Messages: 1834 Registered: July 2009 |
Senior Member |
|
|
In order to raise the preUpdate events, the object's changes must be computed, if you then change the object again in this event, then new changes will not be picked up (as this would require us re-computing the changes again, which would be twice as much work, but might be a useful option to have).
You can use the DescriptorEvent preUpdate instead of the JPA event, this will give you access to the DescriptorEvent object, which has an API, updateAttributeWithObject(), which will update the object, row and change set.
James : Wiki : Book : Blog
|
|
|
|
| Re: Problems with @PreUpdate and flush() [message #663762 is a reply to message #499756] |
Wed, 06 April 2011 08:47   |
|
| Thomas wrote on Tue, 24 November 2009 07:32 | James,
thanks for that. I was trying to avoid any provider specific API and isn't JPA's preUpdate event intended to give an EntityListener the chance to modify the updated objects? Mind you, that it all works fine if you don't do a flush() and end the transaction normally. So I think it's a bug with EclipseLink's flush.
Cheers
Thomas
|
Is still happening with the latest EclipseLink ?
Will the following code work as intended ?
@PreUpdate
@PrePersist
public void updateTimeStamps() {
lastModified = new Date();
if (dateCreated==null) {
dateCreated = new Date();
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.10282 seconds