views:

40

answers:

2

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.

+1  A: 

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>
Torkel
+1  A: 

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

Corey Coogan