-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Is the order of collection events determinable?
PostPosted: Thu Jan 21, 2010 1:47 pm 
Newbie

Joined: Thu Sep 21, 2006 6:14 am
Posts: 4
Hello,

I need to know if the order of created events for collection, such as PostCollectionUpdateEvent is determinable.

Let me draw my scenario:

I've got a collection that represents a unidirectional one-to-many relationship, a classic parent/child relationship.

I'd would create the parents p1 and p2.
And the child c1 and c2.
and define the relations p1 -> c1 and p2 -> c2
and flush()

now, if I remove c1 from p1 and add to p2, I'd get a PostCollectionUpdateEvent for p1 and one for p2 noticing the removal or adding.

Is the order of this events deterministic?

For this events Hibernate would generate SQL statments like followings :

update simpleChild set id_parent=null where id_parent=? and id_simplechild=?
update simpleChild set id_parent=? where id_simplechild=?

Do they always have the same order?

Thanks for any hints

Kuno


Top
 Profile  
 
 Post subject: Re: Is the order of collection events determinable?
PostPosted: Fri Jan 22, 2010 9:28 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

I assume that the order is deterministic.
Take a look at the implementation of org.hibernate.engine.ActionQueue.java and convince yourself:
the collection updates are enqueued into a ArrayList with the add method,
which appends the specified element to the end of this list.
The executeAction method then performs the actions in the same order as they were enqueued.

Code:
private ArrayList collectionUpdates;

...
public void addAction(CollectionUpdateAction action) {
      collectionUpdates.add( action );
   }
...

private void executeActions(List list) throws HibernateException {
      int size = list.size();
      for ( int i = 0; i < size; i++ ) {
         execute( ( Executable ) list.get( i ) );
      }
      list.clear();
      session.getBatcher().executeBatch();
   }


     public void executeActions() throws HibernateException {
      executeActions( insertions );
      executeActions( updates );
      executeActions( collectionRemovals );
      executeActions( collectionUpdates );
      executeActions( collectionCreations );
      executeActions( deletions );
   }



Exception is when a collection is created or removed. These actions are enqueued in separate arraylists
(see lines abobe).


Top
 Profile  
 
 Post subject: Re: Is the order of collection events determinable?
PostPosted: Mon Jan 25, 2010 6:58 am 
Newbie

Joined: Thu Sep 21, 2006 6:14 am
Posts: 4
Hi,

thanks for your reply. I'd like to explain my problem more precisely, since I think my question wasn't very clear.

I need to reproduce every db change to a journaling system using the hibernate events.

In the draw scenario below, hibernate would create two collection events, since there are two modified collections.

For event 1, it would do following
update simpleChild set id_parent=null where id_parent=1 and id_simplechild=1

For event 2, it would do following
update simpleChild set id_parent=2 where id_simplechild=1

The journaling system doens't allow conditionals for foreign keys like id_parent. For this reason, the order of event 1 and event 2 is important. Otherwise, I'd risk to overwrite the changed foreign key with null.

Therefore, I need to know if the order of those events is always like above or if it could also be reversed. Later would mean an unvalid journal entry.

I guess such a order isn't garantied, since this would mean that the action queue would be ordered according to the dependency hierarchy.


Thanks a lot.

Kuno


Top
 Profile  
 
 Post subject: Re: Is the order of collection events determinable?
PostPosted: Mon Jan 25, 2010 8:42 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
The order of collection-update events is chronologic and therefore deterministic
and given that you:

1.) remove c1 from p1

and then

2.) add c1 to p2

then you will have the SQL-statements executed in the order as you reported.
There's no need to be ordered according to the dependency hierarchy.

Guenther


Top
 Profile  
 
 Post subject: Re: Is the order of collection events determinable?
PostPosted: Mon Jan 25, 2010 10:12 am 
Newbie

Joined: Thu Sep 21, 2006 6:14 am
Posts: 4
Hello,

I cannot control how the changes are reported on hibernate API, since the developers are working in different departments. Therefore, for my needs the behavior should be independent on the report order.

Futhermore, I noticed that your conclusion about ordering was not excactly right. In my tests I found out, that the order depends on the order the entities are loaded in the session. Following would reverse the order:
parent2 = (SimpleParent) session.get(SimpleParent.class, idParent2);
parent1 = (SimpleParent) session.get(SimpleParent.class, idParent1);

Anyway, the current behavior is not deterministic in the way I'd like to use it.

Thanks a lot for your help.

Kuno


Top
 Profile  
 
 Post subject: Re: Is the order of collection events determinable?
PostPosted: Mon Jan 25, 2010 10:44 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Sorry Kunt,

I was wrong with my presumption.
I oversight that the collection updates are not enqueued immediately,
apparently the are enqueued during flush where the chronologic order is'nt guaranteed anymore.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.