Yes, this works, but this kind of listener is fired before NH performs
dirty-checks etc contrary to IPreInsert/Update events. So it's being fired
also for entities which are not dirty and there won't be any insert/update
SQL executed for them in standard conditions. It can cause needless
TimeStamp changes like in this case:
var p = session.Load<Post>(123);
ModifyPostAccordingToUserInput(p); //ch changes here
session.SaveOrUpdate(p); // => p.TimeStamp modified as SQL update executed
even though the Post didn't change in fact
I use simmilar code now as a workaround for my previously mentioned
problems, but I added a dirty check so it looks like this:
public class SetModificationDateListener: DefaultSaveOrUpdateEventListener
{
...
protected override object PerformSave(object entity, object id,
IEntityPersister persister, bool useIdentityColumn, object anything,
IEventSource source, bool requiresImmediateIdAccess)
{
SetModificationDateIfPossible(entity);
return base.PerformSave(entity, id, persister, useIdentityColumn, anything,
source, requiresImmediateIdAccess);
}
protected override void PerformUpdate(SaveOrUpdateEvent @event, object
entity, IEntityPersister persister)
{
if (@event.Session.IsDirtyEntity(@event.Entity))
{
SetModificationDateIfPossible(entity);
}
base.PerformUpdate(@event, entity, persister);
}
protected override object PerformSaveOrUpdate(SaveOrUpdateEvent @event)
{
if (@event.Session.IsNewEntity(@event.Entity) ||
@event.Session.IsDirtyEntity(@event.Entity))
{
SetModificationDateIfPossible(@event.Entity);
}
return base.PerformSaveOrUpdate(@event);
}
...
}
I'd still better like to use the IPre-event listeners...
On Mon, Feb 21, 2011 at 3:27 PM, Ricardo Peres <[email protected]> wrote:
> Filip,
>
> This works:
>
> public class SaveListener: DefaultSaveEventListener
> {
> protected override Object PerformSaveOrUpdate(SaveOrUpdateEvent
> @event)
> {
> if (@event.Entity is Post)
> {
> Post post = (@event.Entity as Post);
>
> if (post.ID == 0)
> {
> //new entity
> }
>
> post.Timestamp = DateTime.Now;
> }
>
> return base.PerformSaveOrUpdate(@event);
> }
> }
>
>
>
> On Feb 21, 7:32 am, Filip Kinský <[email protected]> wrote:
> > Hello Fabio, thanks for your response. I just tried to remove the row you
> > mentioned, but it didn't help - the test still fails..
> >
> >
> >
> >
> >
> >
> >
> > On Fri, Feb 18, 2011 at 3:02 PM, Fabio Maulo <[email protected]>
> wrote:
> > > remove this
> > > persister.SetPropertyValue(state, "LastModified",
> trackable.LastModified);
> >
> > > On Thu, Feb 17, 2011 at 5:11 AM, Filip Kinsky <[email protected]>
> wrote:
> >
> > >> I just figured out that if I
> > >> hook
> SaveEventListeners+UpdateEventListeners+SaveOrUpdateEventListeners
> > >> instead of PreInsertEventListeners+PreUpdateEventListeners than it
> starts to
> > >> work - the test passes. So I wonder why everyone suggests to use the
> > >> pre-insert/update events (eg.
> > >>http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventl.
> ..
> > >> Or is this just some bug in NH 3.0.0.1003 pre-insert/update events
> > >> implementation and the test case in my first post should work?
> >
> > >> --
> > >> You received this message because you are subscribed to the Google
> Groups
> > >> "nhusers" group.
> > >> To post to this group, send email to [email protected].
> > >> To unsubscribe from this group, send email to
> > >> [email protected].
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/nhusers?hl=en.
> >
> > > --
> > > Fabio Maulo
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected].
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.