数据库
首页 > 数据库> > NHibernate IPreUpdateEventListener,IPreInsertEventListener未保存到数据库

NHibernate IPreUpdateEventListener,IPreInsertEventListener未保存到数据库

作者:互联网

我正在尝试对我的实体实施简单的审核.可审核实体实现ITimestampable接口,该接口定义DateAdded和DateModified属性.

我创建并注册了一个事件侦听器以填充这些值.这是完整的代码.

internal class TimeStampEventListener : IPreUpdateEventListener, IPreInsertEventListener
    {
        public bool OnPreUpdate(PreUpdateEvent e)
        {
            if (e.Entity is ITimestampable)
            {
                (e.Entity as ITimestampable).DateModified = DateTime.Now;
            }

            return false;
        }

        public bool OnPreInsert(PreInsertEvent e)
        {
            if (e.Entity is ITimestampable)
            {
                (e.Entity as ITimestampable).DateAdded = DateTime.Now;
            }

            return false;
        }

        public void Register(Configuration configuration)
        {
            configuration.SetListener(ListenerType.PreInsert, this);
            configuration.SetListener(ListenerType.PreUpdate, this);
        }
    }

现在,当我进行会话刷新时,将调用侦听器,正确设置了审计属性,但是大多数情况下它们没有保存到数据库中.所谓“大部分时间”,是指很少真正保留这些值.我不确定,但是看起来像在启动应用程序后的第一次插入/更新,这更加奇怪.

当然,首先我要对实体进行更改,更改会保留下来,但审计属性不会.

当我在探查器中查看生成的SQL时,我看到查询中发送的是NULL而不是当前时间,因此我猜测这不是数据库问题.顺便说一句.我正在使用MySQL,并且DateAdded和DateModified列是DATE类型.

在NHibernate站点上,这些属性仅映射为< property>.也许我缺少针对此类情况的“特殊”映射…?

我完全坚持这一点.任何帮助都将受到赞赏.

解决方法:

在Ayende的这篇文章中,答案将被隐藏/显示.

> NHibernate IPreUpdateEventListener & IPreInsertEventListener

…Here comes the subtlety, however. We cannot just update the entity state. The reason for that is quite simple, the entity state was extracted from the entity and place in the entity state, any change that we make to the entity state would not be reflected in the entity itself. That may cause the database row and the entity instance to go out of sync, and make cause a whole bunch of really nasty problems that you wouldn’t know where to begin debugging.

You have to update both the entity and the entity state in these two event listeners (this is not necessarily the case in other listeners, by the way). Here is a simple example of using these event listeners:

这是同一篇文章中显示如何操作的代码:

public class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener
{
    public bool OnPreUpdate(PreUpdateEvent @event)
    {
        var audit = @event.Entity as IHaveAuditInformation;
        if (audit == null)
            return false;

        var time = DateTime.Now;
        var name = WindowsIdentity.GetCurrent().Name;

        Set(@event.Persister, @event.State, "UpdatedAt", time);
        Set(@event.Persister, @event.State, "UpdatedBy", name);

        audit.UpdatedAt = time;
        audit.UpdatedBy = name;

        return false;
    }

    public bool OnPreInsert(PreInsertEvent @event)
    {
        var audit = @event.Entity as IHaveAuditInformation;
        if (audit == null)
            return false;


        var time = DateTime.Now;
        var name = WindowsIdentity.GetCurrent().Name;

        Set(@event.Persister, @event.State, "CreatedAt", time);
        Set(@event.Persister, @event.State, "UpdatedAt", time);
        Set(@event.Persister, @event.State, "CreatedBy", name);
        Set(@event.Persister, @event.State, "UpdatedBy", name);

        audit.CreatedAt = time;
        audit.CreatedBy = name;
        audit.UpdatedAt = time;
        audit.UpdatedBy = name;

        return false;
    }

这是神奇的Set()

private void Set(IEntityPersister persister, object[] state
       , string propertyName, object value)
{
    var index = Array.IndexOf(persister.PropertyNames, propertyName);
    if (index == -1)
        return;
    state[index] = value;
}

标签:mysql,orm,nhibernate,c-2
来源: https://codeday.me/bug/20191013/1908061.html