Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problems with @PreUpdate and flush()(Problems with @PreUpdate and flush())
Problems with @PreUpdate and flush() [message #498894] Thu, 19 November 2009 04:37 Go to next message
Thomas  is currently offline Thomas Friend
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 #498940 is a reply to message #498894] Thu, 19 November 2009 09:57 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 825
Registered: July 2009
Senior Member
I experienced something similar, which had to do with the fact that these events are called when the object itself is being processed. This can mean that other related objects already where inserted/updated and that changes made to those objects simply are ignored. The events are not fired "en masse" before the actual persisting starts, but during the process. Maybe this is related.



Thomas wrote:
> 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 #499127 is a reply to message #498940] Fri, 20 November 2009 00:36 Go to previous messageGo to next message
Thomas  is currently offline Thomas Friend
Messages: 8
Registered: November 2009
Junior Member
Were you able to solve this problem?

I've tracked down the problem where the flush explicitly ignores changes by any events that happened to updated entities (DatabaseQueryMechanism.updateObjectForWriteWithChangeSet(), line 1080 in if uowChangeSet.isChangeSetFromOutsideUOW()) - for some reason it is working when the entities are newly persisted.

After isolating the problem in a small testcase I found the Hibernate does not have this problem.

Cheers
Thomas
Re: Problems with @PreUpdate and flush() [message #499144 is a reply to message #499127] Fri, 20 November 2009 05:58 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 825
Registered: July 2009
Senior Member
I decided to use an application level event; so before committing I made sure that my application called the logic that normally would be in the @PreUpdate. ATM I could also let my ExtendedEntityManager generate such an event.

Tom


Thomas wrote:
> Were you able to solve this problem?
>
> I've tracked down the problem where the flush explicitly ignores changes
> by any events that happened to updated entities
> (DatabaseQueryMechanism.updateObjectForWriteWithChangeSet(), line 1080
> in if uowChangeSet.isChangeSetFromOutsideUOW()) - for some reason it is
> working when the entities are newly persisted.
>
> After isolating the problem in a small testcase I found the Hibernate
> does not have this problem.
>
> Cheers
> Thomas
Re: Problems with @PreUpdate and flush() [message #499690 is a reply to message #498894] Mon, 23 November 2009 18:05 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
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 : Twitter
icon5.gif  Re: Problems with @PreUpdate and flush() [message #499756 is a reply to message #499690] Tue, 24 November 2009 00:32 Go to previous messageGo to next message
Thomas  is currently offline Thomas Friend
Messages: 8
Registered: November 2009
Junior Member
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
Re: Problems with @PreUpdate and flush() [message #663762 is a reply to message #499756] Wed, 06 April 2011 12:47 Go to previous messageGo to next message
Hendy Irawan is currently offline Hendy IrawanFriend
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

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();
    }
}
Re: Problems with @PreUpdate and flush() [message #663966 is a reply to message #663762] Thu, 07 April 2011 00:46 Go to previous messageGo to next message
Thomas  is currently offline Thomas Friend
Messages: 8
Registered: November 2009
Junior Member
Hi Hendy,

Haven't tried it myself anymore, but had raised a bug for Eclipselink: https://bugs.eclipse.org/bugs/show_bug.cgi?id=295665
According to that ticket it's been fixed from 1.2.1 onwards.

Cheers
Thomas
Re: Problems with @PreUpdate and flush() [message #664347 is a reply to message #663966] Fri, 08 April 2011 13:28 Go to previous message
Hendy Irawan is currently offline Hendy IrawanFriend
Messages: 58
Registered: July 2009
Location: Bandung, Indonesia
Member

Thomas wrote on Thu, 07 April 2011 07:46
Hi Hendy,

Haven't tried it myself anymore, but had raised a bug for Eclipselink: https://bugs.eclipse.org/bugs/show_bug.cgi?id=295665
According to that ticket it's been fixed from 1.2.1 onwards.

Cheers
Thomas


Indeed, I've tried it with EclipseLink on GlassFish 3.1 and code in @PreUpdate can modify the entity just fine Smile
Previous Topic:JAXB webservice exception
Next Topic:UnitOfWork.beginEarlyTransaction not having expected effect
Goto Forum:
  


Current Time: Fri Oct 11 18:12:48 GMT 2024

Powered by FUDForum. Page generated in 0.04836 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top