tracking Object changes with nhibernate

902 views
Skip to first unread message

Arman

unread,
May 27, 2009, 5:15:35 AM5/27/09
to nhusers
as you know nhibernate use Unit Of Work pattern to track the object
changes for making the update and insert sql commands.

what i want to do is how can i know about which property have been
changed during a session and log them in a table.

i know log4net produce a good logs , but i don't think so that log4net
provide me some information about which properties is going to be
updated in this session ( as user changed them in UI ).

i want to know if nhibernate have any feature for that , something
like event , or some report about how an object changed and updated.
there is property name IsDirty in session , but i want to know how
dirty it is ??

thanks a lot,

Craig van Nieuwkerk

unread,
May 27, 2009, 5:28:09 AM5/27/09
to nhu...@googlegroups.com
Yes, you need to have a look at event listeners.

http://www.google.com.au/search?q=nhiberate+event+listeners+audit

Arman

unread,
May 27, 2009, 7:23:28 AM5/27/09
to nhusers
thank U bro,
solved my problem
i found this Codeproject Article very helpful, just want to share it
for others:

http://www.codeproject.com/KB/cs/NHibernate_IInterceptor.aspx

thanks again.

On May 27, 12:28 pm, Craig van Nieuwkerk <crai...@gmail.com> wrote:
> Yes, you need to have a look at event listeners.
>
> http://www.google.com.au/search?q=nhiberate+event+listeners+audit
>

Fabio Maulo

unread,
May 27, 2009, 9:03:15 AM5/27/09
to nhu...@googlegroups.com
Take care with that article.... take a look to its date: 2007/02/06

2009/5/27 Arman <arman....@gmail.com>



--
Fabio Maulo

Arman

unread,
May 30, 2009, 2:08:16 AM5/30/09
to nhusers
actually i got with a new problem,

the interceptor is a good idea , but i can not find out dirty
properties with it's methods. there is a FindDirty and OnFLushDirty
method , and also there is previousState parameter on it , but this
parameter always is null !!! and there is nothing to be compared in
FindDirty method to find out what property changed. i know NHibernate
store object state in session , i want to somehow access to it and for
example log this " User a Changed the Lastname of Person x".

any idea ?

Fabio Maulo

unread,
May 30, 2009, 9:39:09 AM5/30/09
to nhu...@googlegroups.com
If you like interceptor OnFLushDirty has all you need.

2009/5/30 Arman <arman....@gmail.com>



--
Fabio Maulo

Arman

unread,
May 30, 2009, 1:17:05 PM5/30/09
to nhusers
actually OnFlushDirty is good but as i said it's PreviousState
parameter is null ,
i know that there is a parameter named "previousState" and a parameter
named "currentState" , i should compare them together to findOut dirty
properties , but it's null !!! and in some cases that it is not null ,
it is the same as the currentState !
so i confused !

On May 30, 4:39 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> If you like interceptor OnFLushDirty has all you need.
>
> 2009/5/30 Arman <arman.naj...@gmail.com>

Craig van Nieuwkerk

unread,
May 30, 2009, 8:13:46 PM5/30/09
to nhu...@googlegroups.com
Email me on cra...@gmail.com and I will send you some code that works.

Arman

unread,
May 31, 2009, 3:27:03 AM5/31/09
to nhusers
thanks craig ,

so any idea why previousState is null ?

On May 31, 3:13 am, Craig van Nieuwkerk <crai...@gmail.com> wrote:
> Email me on crai...@gmail.com and I will send you some code that works.

Craig van Nieuwkerk

unread,
May 31, 2009, 3:29:55 AM5/31/09
to nhu...@googlegroups.com
Not sure, but if you see the code I emailed you it has the previous state.

Arman

unread,
May 31, 2009, 4:23:56 AM5/31/09
to nhusers
i did it as you show me on your code , but what do you think !?
OldState is null !!!!!!

something should be wrong in my code..

On May 31, 10:29 am, Craig van Nieuwkerk <crai...@gmail.com> wrote:
> Not sure, but if you see the code I emailed you it has the previous state.
>

Tuna Toksoz

unread,
May 31, 2009, 4:26:00 AM5/31/09
to nhu...@googlegroups.com
a test case would be cool.

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike

Arman

unread,
May 31, 2009, 7:45:24 AM5/31/09
to nhusers
what kind of test ?

On May 31, 11:26 am, Tuna Toksoz <tehl...@gmail.com> wrote:
> a test case would be cool.
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike

Tuna Toksoz

unread,
May 31, 2009, 7:54:26 AM5/31/09
to nhu...@googlegroups.com
Oh, never mind, was thinknig something else.

Arman

unread,
Jun 1, 2009, 4:00:24 AM6/1/09
to nhusers
so any other idea why previousState is null ?


On May 31, 2:54 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> Oh, never mind, was thinknig something else.
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike
>
> On Sun, May 31, 2009 at 2:45 PM, Arman <arman.naj...@gmail.com> wrote:
>
> > what kind of test ?
>
> > On May 31, 11:26 am, Tuna Toksoz <tehl...@gmail.com> wrote:
> > > a test case would be cool.
>
> > > Tuna Toksöz
> > > Eternal sunshine of the open source mind.
>
> >http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitt...

MattF

unread,
Jun 1, 2009, 4:59:35 AM6/1/09
to nhusers
i have the same problem when creating a IPostUpdateEventListener
(allthough i assume they are all the same).
OldState is always null.

did you create a Jira issue? (i couldn't find one)

this is actually holding us up from making our deadline to pass the
product to the testers

On Jun 1, 6:00 pm, Arman <arman.naj...@gmail.com> wrote:
> so any other idea why previousState is null ?
>
> On May 31, 2:54 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
>
>
>
> > Oh, never mind, was thinknig something else.
>
> > Tuna Toksöz
> > Eternal sunshine of the open source mind.
>

MattF

unread,
Jun 1, 2009, 6:12:41 AM6/1/09
to nhusers
i was having the same problem. but then i made a unit test to try and
reproduce it, and it was working fine.

are you using Active Record?
are you using the latest build of NHibernate?

On Jun 1, 6:00 pm, Arman <arman.naj...@gmail.com> wrote:
> so any other idea why previousState is null ?
>
> On May 31, 2:54 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
>
>
>
> > Oh, never mind, was thinknig something else.
>
> > Tuna Toksöz
> > Eternal sunshine of the open source mind.
>

Arman

unread,
Jun 1, 2009, 11:37:30 AM6/1/09
to nhusers
actually i am using it with nhibernate version 1.0.0 but i tested it
with 2.0.1 version too , both with interceptor and with event
listeners,
in all situations it is null,

i was wondering nobody else face this problem yet ??

Fabio Maulo

unread,
Jun 1, 2009, 11:59:01 AM6/1/09
to nhu...@googlegroups.com
Are you checking an session.Update(entity) with a new fresh session ?
I mean... are you using session.Update(entity) to reattach an entity to a new session ?

2009/6/1 Arman <arman....@gmail.com>



--
Fabio Maulo

Arman

unread,
Jun 1, 2009, 12:17:17 PM6/1/09
to nhusers
in fact , i have a GETALLSTUDENT method that open a session , get all
students , and the close the session,
then when user hit the OK button , i open a new session , and update
the updated student in that , and close it again.

do you mean that i should not close the session in querying ?


On Jun 1, 6:59 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> Are you checking an session.Update(entity) with a new fresh session ?I
> mean... are you using session.Update(entity) to reattach an entity to a new
> session ?
>
> 2009/6/1 Arman <arman.naj...@gmail.com>

Fabio Maulo

unread,
Jun 1, 2009, 12:34:04 PM6/1/09
to nhu...@googlegroups.com
2009/6/1 Arman <arman....@gmail.com>


in fact , i have a GETALLSTUDENT method that open a session , get all
students , and the close the session,
then when user hit the OK button , i open a new session , and update
the updated student in that , and close it again.

do you mean that i should not close the session in querying ?

No, I mean that you can't have the old state in a fresh session. You should use session.Merge to reattach and update or something else to get the previous state (note this ever mean hit a SELECT before update).
--
Fabio Maulo

Fabio Maulo

unread,
Jun 1, 2009, 3:23:42 PM6/1/09
to nhu...@googlegroups.com
This is a new practice... write a request here and before have an answerer create a new JIRA.

The answer is on JIRA; next time, please, avoid this behavior and in general avoid to put links to forum threads in JIRA.
Thanks.

2009/6/1 Fabio Maulo <fabio...@gmail.com>



--
Fabio Maulo

Arman

unread,
Jun 1, 2009, 4:01:40 PM6/1/09
to nhusers
it was not me who wrote that post on JIRA , somebody else pointed to
my topic in here ,

thankU for your reply,
because this project is an old one , it uses NHibernate version
1.0.0 , the session object have the Evict method but i couldn't find
Merge method in that , do it have any equivalence method in this
version ?

and another question , Merge method should be before the change of
properties or before the Update in DB ?

tnx

On Jun 1, 10:23 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> This is a new practice... write a request here and before have an answerer
> create a new JIRA.http://nhjira.koah.net/browse/NH-1807
>
> The answer is on JIRA; next time, please, avoid this behavior and in general
> avoid to put links to forum threads in JIRA.
> Thanks.
>
> 2009/6/1 Fabio Maulo <fabioma...@gmail.com>
>
>
>
> > 2009/6/1 Arman <arman.naj...@gmail.com>

Fabio Maulo

unread,
Jun 1, 2009, 4:14:14 PM6/1/09
to nhu...@googlegroups.com
SaveOrUpdateCopy

2009/6/1 Arman <arman....@gmail.com>



--
Fabio Maulo

Arman

unread,
Jun 1, 2009, 6:08:35 PM6/1/09
to nhusers
excuse me Fabio for asking to much questions,

but what the Lock() and Refresh() methods do ? do they help us in this
way ??


On Jun 1, 11:14 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> SaveOrUpdateCopy
>
> 2009/6/1 Arman <arman.naj...@gmail.com>

Fabio Maulo

unread,
Jun 1, 2009, 6:41:50 PM6/1/09
to nhu...@googlegroups.com
Lock yes and then Update
but SaveOrUpdateCopy, and then you should use the entity returned by SaveOrUpdateCopy, is doing both work.
Take a look to this test .

2009/6/1 Arman <arman....@gmail.com>



--
Fabio Maulo
Reply all
Reply to author
Forward
0 new messages