-->
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.  [ 8 posts ] 
Author Message
 Post subject: IInterceptor Order of Operations
PostPosted: Thu Jun 29, 2006 6:42 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Can someone confirm the order of operations for IInterceptor when calling SaveOrUpdate on a root object?

Is this correct?


[list=]
IsUnsaved()
OnSave()
PreFlush()
FindDirty()
OnFlushDirty()
PostFlush()
[/list]

The reason I ask is because I am trying to modify an object before it is saved to the database (for auditing purposes) and the breakpoint at OnSave() is not being hit. However, the breakpoint at OnFlushDirty() is.

I'm not sure what the cause of this is.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 6:44 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
hrm. Maybe I just answered my own question.

Is OnSave() called only for inserts? If so, where do I capture update events?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 3:30 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
OnSave is only called when the object is first saved to the session using Save() - or SaveOrUpdate() if the object isn't saved before the call (doesn't have an Id yet). It's not about the database operation, just the saved status of the object in relation to the session.

If you want to capture updates to the DB specifically...I'm not sure you can do that easily - I certainly haven't found a way.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 7:32 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I don't mean to capture the actual update command, I just want to set a few properties.

On Save:
-DateCreated = DateTime.Now;
-CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;

On Update:
-DateUpdated = DateTime.Now;
-UpdatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;



That's all I want to do. I thought I could implement OnSave and check if the object is new or not, then fill out the fields accordingly.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 8:41 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
If I call:

Code:
session.SaveOrUpdate(blog);


will it hit OnSave() in my interceptor for blog and all of blog's posts?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 10:43 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
OnSave() will be called if you explicitly call Save() on the session. If the object has already been saved before then Save() with throw an exception, so you'll never get to OnSave().

OnUpdate will be called if you explicitly call Update() on the session. If the object is already a member of the session an exception will be thrown and OnUpdate() will never be reached.

Using SaveOrUpdate() will call OnSave() if the object does not have an identifier and OnUpdate() will be called if the object does have an identifier.

If you have a collection with cascade=save-update then any children that don't have an identifier will go to OnSave() and any children than do have an identifier that are not members of the session will hit OnUpdate(). Any children that have identifiers and are already session members will not hit either OnSave() or OnUpdate().

I'm pretty sure that's how it works...

Anyway, I hope that's clearer.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 12:12 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
There is no OnUpdate method of IInterceptor. There is in ILifecycle, however this introduces persistence information into my domain objects and that is a no-go for this project.

I actually got the thing working. OnSave() is called for transient objects that need to be saved (ie: a sql INSERT). OnFlushDirty() is called when NHib detects that the object's state has changed and needs to issue an UPDATE. So I added my DateCreated/CreatedBy injection in OnSave() and the DateUpdated/UpdatedBy injection in OnFlushDirty().

This works just how I need it to. Thanks for the response.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 5:19 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Ah, sorry about that - I've been using ILifecycle lately in conjunction with interceptors (don't ask!) and mixed them up - I meant OnFlushDirty(). Oops!

Symon.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.