Problems with NHibernate and DateTime mappings

﹥>﹥吖頭↗ 提交于 2019-12-05 12:13:24

I solved my problem and yours by overriding the following NHibernate Class. The NHProfiler will still display a TIMESTAMP value but the underlying parameter will be of type DATE - which will be indexable if your oracle column is also a DateTime. Hover your mouse over the parameters in NHProfiler to see what I mean - something like: :p0: TIMESTAMP - '2011-07-01 00:00:00.00' Date instead of :p0: TIMESTAMP - '2011-07-01 00:00:00.00' Timestamp.

I'm also using Oracle 11g, C# .Net 4, Nhibernate 3.2.

public class OracleDataClientDriver2 : OracleDataClientDriver
{
    protected override void InitializeParameter(IDbDataParameter dbParam, string name, SqlType sqlType)
    {
        switch (sqlType.DbType)
        {
                //Timestamp columns not indexed by Oracle 11g date columns. - Use Date 
            case DbType.DateTime:
                base.InitializeParameter(dbParam, name, SqlTypeFactory.Date);
                break;
            default:
                base.InitializeParameter(dbParam, name, sqlType);
                break;
        }
    }
}

There shouldn't be any conversion at all if the parameters are of the correct type. TIMESTAMP or to_date() is only needed if you have your timestamp in string type. If the parameter is correctly typed it should read like this:

select
       tab.COL as col
   from
       TABLE tab
   where
       tab.COL = :p0;
:p0 = 01.01.2000 00:00:00 [Type: DateTime (0)]

So make sure you are passing DateTime objects to the query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!