My workaround and issue...
The Task
I recently worked on an object which represents an uploaded file. We wanted this object to have an
uploadDateTime field to store the date at which the record was created.
The Solution
I got this to work by setting up the database DDL thus:
Code:
updateDateTime DateTime NOT NULL DEFAULT GETDATE()
I updated the NHibernate mapping for that column:
Code:
<property column="uploadDateTime" type="DateTime" name="UploadDateTime" not-null="true" insert="false" update="false"
The
insert="false" update="false" prevents NHibernate from using that field in INSERT and UPDATE statements, thereby forcing the database to generate the default value.
I wrote a unit test that saved down the object and then queried the database for that object and found that the
uploadDateTime field had been populated. Success!
However...
The Problem
I noticed that upon saving a object, the property isn't automatically populated. This is because, although the record is created in the database and the field is populated, the field value is not then synchronised back up with the object!
The Workaround
I'm going to populate this field in my DAO layer before insertion.
The Feature Request
Couldn't NHibernate recognise that it would need to synchronise the field after insertion upon seeing the following combination of column attributes?
- insert="false"
- not-null="true"