我正在尝试为我的NHibernate实体实现一个自动LastUpdate属性。我想过使用数据库触发器,然后认为在NHibernate中做得更好。我正在关注此blog post作为指南。
我的域名实体是:
public class Instrument : Entity, IAuditableEntity
{
public virtual string Name { get; set; }
public virtual string Symbol {get; set;}
public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; }
public virtual bool IsActive { get; set; }
}
继承自
public abstract class Entity
{
public virtual int Id { get; set; }
public virtual int Version {get; private set;}
public virtual DateTime CreateDate { get; private set; }
public virtual DateTime UpdateDate { get; set; }
}
这是界面:
public interface IAuditableEntity
{
DateTime UpdateDate { get; set; }
}
我的听众课程:
public class CustomUpdateEventListener : DefaultSaveEventListener
{
protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
{
var entity = evt.Entity as IAuditableEntity;
if (entity != null)
{
ProcessEntityBeforeUpdate(entity);
}
return base.PerformSaveOrUpdate(evt);
}
internal virtual void ProcessEntityBeforeUpdate(IAuditableEntity entity)
{
entity.UpdateDate = DateTime.Now;
}
}
和我的域实体Instrument
映射文件:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MooDB" namespace="MooDB" >
<class name="MooDB.Domain.Instrument,MooDB" table="instruments">
<id name="Id" column="instrumentId" unsaved-value="0">
<generator class="increment"></generator>
</id>
<version name="Version" column="version" type="integer" unsaved-value="0"/>
<property name="Symbol" column="symbol" type="string" length="10" not-null="false"/>
<property name="Name" column="`name`" type="string" length="30" not-null="false"/>
<property name="IsActive" column="isActive" type="bool" not-null="true" />
<set name="BrokerInstruments" table="brokerInstruments" generic="true" inverse="true">
<key column="instrumentId" />
<one-to-many class="MooDB.Domain.BrokerInstrument"/>
</set>
<property name="CreateDate" column="created" not-null="false" />
<property name="UpdateDate" column="lastUpdated" />
</class>
</hibernate-mapping>
我的hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
... connection details snipped
<mapping assembly="MooDB"/>
<listener class="MooDB.Data.CustomUpdateEventListener, MooDB.Data" type="update" />
</session-factory>
</hibernate-configuration>
我的解决方案构建正常,但当NHibernate试图找到列表器时,我得到FileNotFoundException
。我的引用设置正确,我有正确的using
语句,所以我不确定为什么NHibernate找不到它。这是日志中的错误:
2012-01-15 15:26:12,911 [TestRunnerThread]错误 NHibernate.Util.ReflectHelper [(null)] - 无法加载类型 MooDB.Data.CustomUpdateEventListener,MooDB.Data。 System.IO.FileNotFoundException:无法加载文件或程序集 'MooDB.Data'或其依赖项之一。系统找不到了 文件指定。文件名:'MooDB.Data'
你知道可能出现什么问题吗?
修改:我现在收到以下SqlTypeException
:
2012-01-15 17:06:37,302 [TestRunnerThread]错误NHibernate.AdoNet.AbstractBatcher [(null)] - 无法执行命令:UPDATE instruments SET version = @ p0,symbol = @ p1,[name] = @ p2,isActive = @ p3,created = @ p4,lastUpdated = @ p5 WHERE instrumentId = @ p6 AND version = @ p7 System.Data.SqlTypes.SqlTypeException:SqlDateTime overflow。必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间。
似乎entity.UpdateDate = DateTime.Now;
中的代码ProcessEntityBeforeUpdate
由于某种原因未运行。有什么想法吗?
答案 0 :(得分:1)
看起来你的装配名称是错误的。试试这个:
<listener class="MooDB.Data.CustomUpdateEventListener, MooDB" type="update" />