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.  [ 3 posts ] 
Author Message
 Post subject: How to trap an automatic immediate UPDATE after a SELECT?
PostPosted: Wed May 14, 2008 5:04 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Hi,

This is apparantly a desired behaviour of NHibernate (1.2): If you load an object, immediately after loading NHibernate checks if the properties are still the same, and if it doesn't, saves it immediately to the database with the updated properties.

This behaviour is quite nice in my opinion, but I want to be able to track it: how can I know when NHibernate does this? Is there some event I can trigger?

Eventually: is there a way to avoid this?


Thanks a lot in advance,


Pieter


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 2:56 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Hi,
look for Interceptor.
Interceptor has methods like FindDirty() and OnFlushDirty() which you can override. There you can even stop the update, when you implement FindDirty() in that way that you say NHibernate "no, there are no changed properties!"...

I do it in a similar way - I wanted the objects saved only when I call the save() or update() methods and not when NHibernate thinks to do so.
So I wrote a property CanSave which is alwas false except I set it true in the save/update methods. In the FindDirty() method of my interceptor I return an empty int[] array if this property is false... in this way NHibernate thinks that nothing has changed...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 10:28 am 
Beginner
Beginner

Joined: Mon Mar 26, 2007 5:47 am
Posts: 22
Hi,

I tried it, but it doesn't work :-( See my code below. I tried it with an empty array, or with an array of the length of the properties, but none of them worked :-( When I debug we pass into the correct part of the method, but still the update to the database happens...

Any idea?

Thanks a lo in advance,

Pieter




My code of the interceptor:

Option Explicit On

Imports NHibernate
Imports NHibernate.Cfg

Public Class NHibernateInterceptor
Inherits EmptyInterceptor

Public Overrides Function FindDirty(ByVal entity As Object, ByVal id As Object, ByVal currentState() As Object, ByVal previousState() As Object, ByVal propertyNames() As String, ByVal types() As NHibernate.Type.IType) As Integer()
'when we are loading an object and some of it's properties are changed: we don't want to have it saved automaticly to the database!
If CType(entity, BaseClass).IsDoingLoad Then
'in this case we have to return an empty array of integers
'Dim intA(propertyNames.Length - 1) As Integer
Dim intA(-1) As Integer
Return intA
Else
'normal case: 'call the MyBase
Return MyBase.FindDirty(entity, id, currentState, previousState, propertyNames, types)
End If
End Function

Public Overrides Function OnLoad(ByVal entity As Object, ByVal id As Object, ByVal state() As Object, ByVal propertyNames() As String, ByVal types() As NHibernate.Type.IType) As Boolean
Dim MyBaseClass As BaseClass = CType(entity, BaseClass)
'method which has to be executed when an object is Loaded from the database
MyBaseClass.BeforeLoad()
'call the MyBase
Return MyBase.OnLoad(entity, id, state, propertyNames, types)
End Function

End Class


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.