Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Descriptor Events on Entities which have not been modified during a Transaction
Descriptor Events on Entities which have not been modified during a Transaction [message #756985] Tue, 15 November 2011 20:33 Go to next message
J F is currently offline J FFriend
Messages: 242
Registered: July 2009
Senior Member
I am trying to understand the order in which Descriptor Events are fired on Entities.
I have created an example to try and understand this.
Say that...
I have a "Container" Entity which contains an ordered list of sub Item Entities.
I wish to preserve the order of this list persistently and I don't wish to use the @OrderColumn(name="xx") annotation.
So I use the @OrderBy annotation and create a persistence field item_idx on which to set up the ordering.
This has to reflect the Items ordder in the Containers list so I create a function z_updateIndices which sets these fields just before the Transaction is committed;
public void z_updateIndices() {
for (int i = 0; i<items.size();i++) {
items.get(i).setItemIndex(i);
}
}
Now suppose it is far too much overhead to walk my persistence tree and call this function every "pre" commit, or everytime I insert an Item, I'd like to use events on an Entity to set up this ordering.
?I cannot do this by placing the @PreUpdate or @PrePersist annotations on the Container as by then Eclipselink may have taken stock of what has changed.
?I have to use a DescriptorEvent method like event.updateAttributeWithObject.
If I use this to call z_updateIndices on my container and then make sure that all Items in the list call event.updateAttributeWithObject on Attribute item_idx then my changes should be reflected.
I have to trap events at the level of Items, as event.updateAttributeWithObject has an implicit object scope.
?I assume the best event to trap is the PreWriteEvent from my reading of the newsgroup.
But I can trap the first Item to get an event and then call z_updateIndices once on its container.
For the others I just have to call event.updateAttributeWithObject on item_idx.
The problem comes that some Items may not have been changed in this transaction and so ?NO EVENT is called on them?
Therefore event.updateAttributeWithObject is not called on them and changes to their item_idx are not persisted and therefore they are not ordered correctly.
However this is not true for all of them, but appears to be true only for the first item in the list as it is when loaded.
Subsequent items that have not changed before z_updateIndices is still have an event called on them.
Interestingly if I reference the items in a @PostLoad method on the Container, this does not occur and all Items have a PreWriteEvent called on them.

I attach a Maven project to show what the problem is.
Note that the persistence.xml has caching switched off, so that caching does not just make everything right, because I cannot always depend on caching?

My question is -
Am I right in thinking that I cannot depend on a DescriptorEvent being called on an Entity which has not changed within a Transaction?
Or is this a bug?
Or am I doing something else wrong?
  • Attachment: jpaTest1.zip
    (Size: 33.52KB, Downloaded 102 times)
Re: Descriptor Events on Entities which have not been modified during a Transaction [message #757805 is a reply to message #756985] Mon, 21 November 2011 18:24 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Why don't you use the @OrderColumn?

Or, can you maintain the index in your model, just update the index every time you add or remove from the collection, such as defining add/remove methods on your class.

For using events, why would you want events from objects that did not change?


James : Wiki : Book : Blog : Twitter
Re: Descriptor Events on Entities which have not been modified during a Transaction [message #758197 is a reply to message #757805] Tue, 22 November 2011 09:59 Go to previous message
J F is currently offline J FFriend
Messages: 242
Registered: July 2009
Senior Member
Thanks again James.


The reason for the example was to see whether there were any other techniques that could be used to manage a collection of objects which derived state from their position in a List. The reason for not doing this at the time of inserting/removing other items is just that it means doing it all the time, not on some final event. Before I rewrote this I wanted to make sure I was not missing something that I could get for free from Eclipselink, I wasn't sure from the docs whether the PreWrite event was available on all Entities or not.

Its not an issue and I can see the benefits/difficulties of calculating the changes once and not firing events on unchanged entities.
Previous Topic:Re: Must static-weaving be set in the persistence.xml file if static weaving has been performed
Next Topic:Re: Descriptor Events on Entities which have not been modified during a Transaction
Goto Forum:
  


Current Time: Mon Dec 22 06:24:14 GMT 2014

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

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