Support for Sql Server datetime2 with NHibernate 3.1.0

Posts   
 
    
costab
User
Posts: 36
Joined: 21-Aug-2010
# Posted on: 30-Nov-2011 20:48:59   

In NHibernate 3.x they added support for datetime2 fields in the sense that the milliseconds are saved and read accurately.

I am currently experiencing a problem where NHibernate is failing to find records that have a datetime2 field part of the primary key.

If I modify the entity hbm.xml file and add type="datetime2" it works fine (datetime2 is the type they added in the MS2008Dialect):

<key-property name="TransitionDate" column="[TransitionDate]" access="field.camelcase-underscore" type="datetime2"/>

How can I map in LLBLGEN Pro these fields to the NHibernate datetime2, so LLBLGENPRO generates the type="datetime2" attribute?

Thanks

costab
User
Posts: 36
Joined: 21-Aug-2010
# Posted on: 30-Nov-2011 23:55:51   

I ended up hacking: C:\Program Files (x86)\Solutions Design\LLBLGen Pro v3.1\Frameworks\NHibernate\Templates\Shared\Shared\generalTemplateUtils.lpt:

At line 361:

    internal static string ProduceTypeConverterTypeString(FieldMapping mapping)
    {
        if(mapping.TypeConverterToUse==null)
        {
        
          if (mapping.MappedFieldTypeAsString.Contains("datetime "))
             return " type=\"datetime2\"";
        
            return string.Empty;
        }
        return string.Format(" type=\"{0}, {1}\"", mapping.TypeConverterToUse.TypeFullName, mapping.TypeConverterToUse.AssemblyName);
    }

I added this block:

          if (mapping.MappedFieldTypeAsString.Contains("datetime "))
             return " type=\"datetime2\"";

I better way might have been to add a type converter but it seems to me a bit of a waste since NHibernate supports directly datetime2.

I also noticed that the llblgen pro doesn't store the db type for each column in the project file so there is really no way to make the distinction between a datetime and datetime2 field. Maybe this was one more reason to use a custom converter.

Any thoughts guys?

Thank you

PS. In case you wonder why the space after datetime:

          if (mapping.MappedFieldTypeAsString.Contains("datetime "))
             return " type=\"datetime2\"";

The mapping object doesn't seem to have a MappedFieldType property so I had to use the MappedFieldTypeAsString look for datetime, but because there is a datetimeoffset type as well I had to add the space.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Dec-2011 12:38:38   

I also noticed that the llblgen pro doesn't store the db type for each column in the project file

It does. Please check the <TargetDatabases> tag and the <MappingsStores> tag.

costab
User
Posts: 36
Joined: 21-Aug-2010
# Posted on: 01-Dec-2011 18:39:29   

Walaa wrote:

It does. Please check the <TargetDatabases> tag and the <MappingsStores> tag.

Yes, it does. Sorry, originally I searched for the string datetime2 and when I couldn't find it I thought llblgen pro doesn't store the db types, but now I noticed that it stores them as numbers instead of strings. That's great then. I will change my code to react only on datetime2 fields.

But what do you think about supporting the datetime2 type for NHibernate?

Thanks

costab
User
Posts: 36
Joined: 21-Aug-2010
# Posted on: 01-Dec-2011 18:46:13   

Walaa wrote:

It does. Please check the <TargetDatabases> tag and the <MappingsStores> tag.

Can you please confirm that the DbType number maps to the SqlDbTypes enumeration constants in the SqlServerDriver?

Thanks

costab
User
Posts: 36
Joined: 21-Aug-2010
# Posted on: 01-Dec-2011 18:58:37   

ok, based on your info I changed the code in the template to:

internal static string ProduceTypeConverterTypeString(FieldMapping mapping)
    {
        if(mapping.TypeConverterToUse==null)
        {
        
          var mappedTarget = mapping.MappedTarget;
        
        
          if (mappedTarget.DbTypeAsString == "datetime2")
             return " type=\"datetime2\"";              
        
            return string.Empty;
        }
        return string.Format(" type=\"{0}, {1}\"", mapping.TypeConverterToUse.TypeFullName, mapping.TypeConverterToUse.AssemblyName);
    }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-Dec-2011 10:53:33   

Is it working correctly?