Reputation: 7228
I'm attempting to implement an automatic LastUpdate attribute to my NHibernate entities. I thought about using database triggers and then thought it better done in NHibernate. I am following this blog post as a guide.
My domain entity is:
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; }
}
which inherits from
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; }
}
and here's the interface:
public interface IAuditableEntity
{
DateTime UpdateDate { get; set; }
}
My listener class:
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;
}
}
and my domain entity Instrument
mapping file:
<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>
My 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>
My solution builds fine but I am getting a FileNotFoundException
when NHibernate tries to find the listner. My references are set up correctly and I have the right using
statements, so I'm not sure why NHibernate can't find it. Here's the error from the log:
2012-01-15 15:26:12,911 [TestRunnerThread] ERROR NHibernate.Util.ReflectHelper [(null)] - Could not load type MooDB.Data.CustomUpdateEventListener, MooDB.Data. System.IO.FileNotFoundException: Could not load file or assembly 'MooDB.Data' or one of its dependencies. The system cannot find the file specified. File name: 'MooDB.Data'
Do you know what could be wrong?
Edit: I'm now getting the following SqlTypeException
:
2012-01-15 17:06:37,302 [TestRunnerThread] ERROR NHibernate.AdoNet.AbstractBatcher [(null)] - Could not execute command: 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. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
It seems that the code entity.UpdateDate = DateTime.Now;
in ProcessEntityBeforeUpdate
is not being run for some reason. Any ideas?
Upvotes: 1
Views: 1273
Reputation: 150253
It's looks like your's assembly name is wrong. try this:
<listener class="MooDB.Data.CustomUpdateEventListener, MooDB" type="update" />
Upvotes: 1