为什么NHibernate OnPreInsert和OnPreUpdate方法都被一个对象调用 [英] Why both NHibernate OnPreInsert and OnPreUpdate methods get called for an object

查看:119
本文介绍了为什么NHibernate OnPreInsert和OnPreUpdate方法都被一个对象调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PreSaveEventListener中使用NHibernate OnPreInsert和OnPreUpdate事件来设置实体的CreatedDate和ModifiedDate.问题是,有两个实体在我第一次创建它们时会触发两个事件. 这会导致问题,因为在OnPreInsert事件之后没有保存实体状态,因此OnPreUpdate事件在全新的实体状态上运行,并且我的CreatedDate从未设置为(默认值为01/01) /0001).

I use the NHibernate OnPreInsert and OnPreUpdate events in a PreSaveEventListener to set the CreatedDate and ModifiedDate of my entities. The problem is, there are two entities for which both events get triggered when I first create them. This causes an issue because the entity state does not get saved after the OnPreInsert event, so the OnPreUpdate event operates on a whole new entity state and my CreatedDate never gets set (defaults to 01/01/0001).

起初,我认为这是因为我的代码在事务结束之前发起了两个SaveOrUpdate调用.果然,我找到了实现此效果的一些代码.但是后来我意识到其他实体仍然在发生这种情况.据我所知,只有这两个实体都有此问题.我通过在其构造函数中设置CreatedDate暂时解决了该问题,但我想避免这种情况.

At first, I thought this was because my code that was initiating two SaveOrUpdate calls back to back before the end of the transaction. Sure enough, I found some code to this effect. But then I realized this was still happening for the other entity. So far as I can tell, only these two entities have this issue. I temporarily solved the problem by setting the CreatedDate in their constructors, but I want to avoid this.

这是我的结构:
业务实体(具有两个具体的连接子类的抽象类)
与业务部门具有多对一关系的BusinessContact实体

Here's my structure:
Business entity (an abstract class that has two concrete joined-subclasses)
BusinessContact entity with a Many-To-One relationship with Business

我最近意识到,它也发生在另一个对象(InvoiceLineItem)上,但是没有实例化和以几乎相同的方式使用的几乎相同的对象(BillLineItem).似乎相当武断.

I have recently realized that it's also happening on one other object (InvoiceLineItem), but not a near identical object (BillLineItem) instantiated and used in near identical ways. Seems rather arbitrary.

以前有人看过吗?

这是事件侦听器代码:

public class PreSaveEventListener : IPreInsertEventListener, IPreUpdateEventListener {
    public bool OnPreInsert(PreInsertEvent @event) {
        EntityWithGuidId entity = @event.Entity as EntityWithGuidId;

        if (null != entity) {
            var createdDate = DateTime.Now;
            var modifiedDate= DateTime.Now;
            Set(@event.Persister, @event.State, "CreatedDate", createdDate);
            Set(@event.Persister, @event.State, "ModifiedDate", modifiedDate);
            entity.CreatedDate = createdDate;
            entity.ModifiedDate = modifiedDate;
        }

        return false;
    }

    public bool OnPreUpdate(PreUpdateEvent @event) {
        EntityWithGuidId entity = @event.Entity as EntityWithGuidId;

        if (null != entity) {
            var modifiedDate= DateTime.Now;
            Set(@event.Persister, @event.State, "ModifiedDate", modifiedDate);
            entity.ModifiedDate = modifiedDate;
        }

        return false;
    }

    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;
    }
}

推荐答案

事件侦听器在我的项目中引起了许多不同的问题,其中许多对我来说没有意义.我认为,当NHibernate在创建实体后真正更新您的实体时,可能会导致您的问题. NHibernate可以更新实体的版本或为其设置一些ID(或GUID).您可以在此处放置已发布实体的映射吗?我也建议您在探查器中查看sql查询.

Event listeners caused a lot of different issues in my project and many of them doesn't make sense to me. I think your issue can be caused in case when NHibernate really updates your entity after it created. NHibernate can update version of entity or set some id (or guid) for it. Can you put here mapping of issued entity? I also will suggest you to look at sql queries in profiler.

这篇关于为什么NHibernate OnPreInsert和OnPreUpdate方法都被一个对象调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆