开发者

How To Make NHibernate Automatically change an "Updated" column

I am applying NHibernate to an existing project, where tables and columns are already defined and fixed.

The database is MS-SQL-2008. NHibernate 2.1.2

Many tables have a column of type timestamp named "ReplicationID", and also a column of type datetime named "UpdatedDT".

I understand I might be able to use the <timestamp> element of the mapping file to describe the "ReplicationID" column which will allow NHibernate to manage that column.

开发者_运维百科

Is it possible to make NHibernate automatically update the UpdatedDT column when the row is updated? I suppose I could try mapping the UpdatedDT property to be of type timestamp, but that have other side effects.


You can use the generated="always" for properties that are set/updated by the database. NHiberante will then issue a select for this column after the insert/update statement.

example:

<property name="CreatedOn" 
          insert="false" update="false" type="Timestamp" generated="insert" />
<property name="UpdatedOn" 
          insert="false" update="false" type="Timestamp" generated="always" />

The same works for version/timestamp:

 <version name="_timeStamp" generated="always" unsaved-value="null" type="BinaryBlob"   access="field">
      <column name="TimeStamp" not-null="false" sql-type="timestamp"/>
 </version>


You need to implement an event listener. I've done it and it's very easy. I recommend using an interface to identify your entities that require updating. This post describes it all:

http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx

http://knol.google.com/k/fabio-maulo/nhibernate-chapter-11/1nr4enxv3dpeq/14#

//fluent registration http://ferventcoder.com/archive/2009/11/18/nhibernate-event-listener-registration-with-fluent-nhibernate.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜