NHibernate and Ms Sql Server 2008: Date, Time, DateTime2 and DateTimeOffset

Ms Sql Server 2008 come with a lot of features talking about Date/Time. Besides the already known DbType.DateTime, now we have new types:

  • Date
  • Time
  • DateTime2
  • DateTimeOffset

NHibernate 2.1 introduce changes to support this types out-of-the-box. This table show all the details about how to configurate your system: classes, mappings:

CLR Type

DbType

Sql Server Type

NHibernate type

System.DateTime

DateTime

datetime

datetime

System.DateTime

DateTime2

datetime2

datetime2

System.DateTime

Date

date

date

System.TimeSpan

Time

time

TimeAsTimeSpan

System.DateTime

Time

time

time

System.DateTimeOffset

DateTimeOffset

datetimeoffset

datetimeoffset

A breaking change was introduced: the old TimeSpan NHibernate type was moved to TimeSpanInt64 (it uses a System.Int64).

When we talk about ?NHibernate type?, we are refering to what you?ve to put into your mappings, i.e: <property name=?OnlineMeeting? type=?datetimeoffset?/> .

To use this features and more you?ve to use the dialect NHibernate.Dialect.MsSql2008Dialect.

The dialect changes include these types and changes to Hql functions that return the current system timestamp.

  • current_timestamp: Now returns the current system timestamp as a DateTime2 value. It uses sysdatetime Sql Server function at background.
  • current_timestamp_offset: New function!. Returns the current system timestamp with the offset as a DateTimeOffset value. It uses sysdatetimeoffset Sql Server function at background.

The idea of this post isn?t to explain every new type and what it can do, otherwise is to show you what have to do in your classes, queries or mappings to configure your date/time types with NHibernate. Besides, to know every detail of these types you can visit this useful .NET Framework Developer’s Guide.

Future<T> Queries with HQL and Criteria

A few days ago Oren Eini and Davy Brion were working in a new feature for NH 2.1 (no a release yet) called Future, in the ICriteria API. You can see and explanation of the job here. Now I?ve committed a complement to enable that use into the IQuery API, for those who prefer to use HQL.

What is Future Query anyway? Future are queries that are kept, waiting to be executed as a group in just one roundtrip, making use of an underlying NHibernate feature: MultiCriteria/MultiQuery.

Let?s have a look to this simple piece of code. You can see both queries sentences, and both are executed in that sentence point against the database producing 2 roundtrips to the database. Nothing weird about this, but what if we can just execute the queries in just one roundtrip?

And now, using Future we can hold the execution, in this case just two, but we can hold how many queries we need.

Where is the trick? Future method is returning a delayed enumerable implementation, that?s all. When you iterate the enumerable (with a foreach for example), it detects and execute all the queries using a NHibernate-MultiQuery command (could be a MultiCriteria, it depends what we are using). But we don?t need to know nothing about the underlying implementation, just the concept.

In other terms, this is what happens behind the scenes:

But what if we want to retrieve an entity or a scalar ? This is too simple for an IEnumerable. Thanks to Davy Brion we have another feature called FutureValue. The mechanism is the same as Future, but instead of expect a IEnumerable, we obtain a single value (an entity or a scalar).

Note: examples adapted from Davy Brion posts [1] and [2].

NHibernate isn’t integrated to the compiler, so what?

I know, this title sound like: “hmm?” Let me explain it with more detail.

NHibernate is a framework that use natively Xml in order to configure a mapping between objects and tables, is in charged of join these different worlds.

But now, what is going wrong with this? Many people can say that Xml files are evil, because you should write a lot of lines, and when you build your project, the compiler doesn’t know if its ok or not, you have none errors. And this is true, you should run your project and see what happen in runtime, and to get an error in runtime is just annoying.

But now, do we have another alternative to this? Yes of course. Lets go to details. NHibernate has a “compiler” too, and you can guess what it is: BuildSessionFactory() method. You don’t have to launch the whole application to know if your mapping is working well. A way to know if everything is ok is create a test like this:

Then you just need to build the project, and run this simple and fool test, if your code pass though this, your mappings are ok.

We should remember one thing, you can map into Xml not just entities, you can map queries too (and another stuff that isn’t part of this matter). To map queries into Xml it calls: Named Queries, and in this post we talk about this matter. But the main point is, mapping the queries you will know if them are well formed when BuildSessionFactory() is raised, besides another beauties discussed in that post.

Another thing you should remember is you MUST use the Xsd to validate the mapping file. This is mandatory if you’re using NHibernate and you want to spend time programming instead of dealing with mapping errors (and here they come even with not-well-formed xmls). But this is topic to an How to or another post.

XUnit 1.0.2 and Resharper 4.1

XUnit doesn’t support yet to run tests on Resharper 4.1 so I decided to build a home-made release.

You can download this XUnit package tested with Resharper 4.1 and with TestDriven 2.14.2190. This is not an official release, you can get the official one in the Codeplex page,use at your risk:

Download

Hug a developer today

nhforge.org is ready !

Ok, this is what we were looking for a long time in the NHibernate community and I think we making it good. These past weekend, after a huge configuration process everybody can say: NHForge is up !

www.nhforge.org

Why nhforge? I think the “welcome” in the home site says enough.

There is something you should do if you are interested in NHibernate,you MUST suscribe to this feed: NHibernate blog.

NHibernate: Almost 2000 downloads on 3 days

In these 3 days NHibernate since the official release NHibernate 2.0 GA reached almost 2000 downloads. IMHO is a nice start for a .Net ORM tool.

What comes with this release

How much has change NHibernate since 1.2.1 till 2.0 GA ?

Patrick Smacchia sent me this link where NHibernate 1.2.1 GA and 2.0 GA are analyzed with NDepend.

I want to you appreciate the analysis made with this tool, to see how much of details it offers to build comparatives between both releases. What I consider the most important is the analysis of the changes made. You will see in the picture with the blue zones, all the refactored parts, and you can arrive to conclusions :)

NHibernate 2.0: Changes Overview

NHibernate Validator 1.0 CR1

I hope you enjoy this brandnew release of this amazing validator framework. Comes with more stability, more documentation, examples, and the most important point is the full compliance with NHibernate 2.0 GA.

Issues as usual must be reported in our issue-tracker.

Binaries, source and examples: download it here !

NHibernate 2.0 G.A. is release !

Enjoy it !

  

 Download NHibernate 2.0