I am trying to get a solution to this problem and kind find a decent answer.
The issue: multiple (most) database objects must have fields for Created and Modified. The date/time assigned cannot be generated by code or within the service processing, the SQL generated by NHibernate needs to populate the appropriate parameter with the appropriate function for the respective database.
As in (pseudo code):
A sample object like Order should have date/time fields for Created and Modified and should NOT be assigned this way: order.Created = DateTime.Now; order.Modified = DateTime.Now;
The reason should be obvious. Some transactions are complex and nested and the date/time actually inserted versus assigned in code (as demonstrated above) would differ.
The appropriate method is to have NHibernate assign the value for the parameter as a function (i.e. GetDate() for SQL sever).
I have seen hints and workarounds and nothing I have found seems right (perhaps I just haven;t found the correct resource).
From my view, I should be able to do something like this in NHibernate (pseudo mapping using Fluent Nhibernate for brevity):
class OrderMap : ClassMap<Order> { public OrderMap() { WithTable("tb_order"); Id(x => x.ID, "pk_order_id").GeneratedBy.Native(); // other mappings ... Map(x => x.Created, "created").GenerateDate(Behavior.OnInsert); Map(x => x.Modified, "modified").GenerateDate(Behavior.Always); } }
I understand that NHibernate and Fluent NHibernate are different animals, but that sample should make the point.
I cannot see how this particular scenario is not common and a fundamental requirement; especially, since it is should be so easy to implement.
I should be able to tell NHibernate that the date should be genertated by the database and specify the behavior (on insert or new, or always). NHibernate requires each connection to specify a provider (like MS Sql Server 2005) and it should be rather simple to associate the function that the particular database supports for inserting a database generated date like GetDate() for SQL Server.
With a mapping like above, NHibernate should then allow a mapping for a readonly public property with a private backing field and should auto-inject into the SQL the proper function based on the behavior specified.
If this is not currently supported, the please consider this elementary and a high priority. In my opinion, a professional database should only allow date/times for Created and Modified to be generated by the database as part of the actual transaction. I could list numerous problems associated to faking the date/time by pre-populating the values as I demonstrated in my first pseudo-code example.
So, again:
1) perhaps this is supported and I am not aware. If it is, only post if you have sample code or a link to sample code. I have searched and have not found an answer, so mentioning it without some helpful guidance is not useful.
2) If what I suspect is true: NHibernate does not support this and what I found really are the only options (which, in my honest opinion, seemed to be some really some complex hacks that may not even solve this issue, I.e. not use database generated dates)... if this is true, how does one get support for such a change? I suspect that most every developer would really want this feature and would go "thank you", "thank you", if it was supported similar to how I described.
TIA
Trevor
|