Casts when using NHibernate and Oracle

1

The problem of typecasting when comparing NHibernate. The error occurs when using Telerik Dynamic Custom Binding using NHibernate and Oracle database.

Here is the class implements the Telerik Custom Binding which works fine with the Entity Framework and MS SQL Server 2008 when used with NHibernate and Oracle database errors casts VARCHAR to NVARCHAR at this in mapinga NHibernate NO Property which specifies the type of Oracle database.

public static class DynamicDataBinding
    {
        public static IQueryable<T> Binding<T>(this IQueryable<T> query, GridCommand cmd, out int total)
        {
            query = query.Filtering(cmd.FilterDescriptors);
            total = query.Count();
            return query.Sorting(cmd.GroupDescriptors, cmd.SortDescriptors).Paging(cmd.Page, cmd.PageSize);
        }

        private static IQueryable<T> Filtering<T>(this IQueryable<T> query, IList<IFilterDescriptor> filters)
        {
            return filters.Any() ? query.Where(ExpressionBuilder.Expression<T>(filters)) : query;
        }

        private static IQueryable<T> Paging<T>(this IQueryable<T> query, int currentPage, int pageSize)
        {
            return ((pageSize > 0 && currentPage > 0) ? query.Skip((currentPage - 1) * pageSize) : query).Take(pageSize);
        }

        private static IQueryable<T> Sorting<T>(this IQueryable<T> query, IEnumerable<GroupDescriptor> groups, IEnumerable<SortDescriptor> sorts)
        {
            query = groups.Any() ? groups.Reverse().Aggregate(query, (cur, group) => cur.AddSortExp(group.SortDirection, group.Member)) : query;
            return sorts.Any() ? sorts.Aggregate(query, (cur, sort) => cur.AddSortExp(sort.SortDirection, sort.Member)) : query;
        }

        private static IQueryable<T> AddSortExp<T>(this IQueryable<T> query, ListSortDirection sort, string member)
        {
            return (sort == ListSortDirection.Ascending) ? query.OrderBy(member) : query.OrderByDescending(member);
        }
    }

Here is an error log, SQL from which the Oracle executed!

12:04:40.125 [11] DEBUG NHibernate.Engine.Query.QueryPlanCache - located HQL query plan in cache (.Count[X4Data.Entity.IIndicatorsHistory](.Where[X4Data.Entity.IIndicatorsHistory](.Where[X4Data.Entity.IIndicatorsHistory](.Where[X4Data.Entity.IIndicatorsHistory](NHibernate.Linq.NhQueryable`1[X4Data.Entity.IIndicatorsHistory], Quote((x, ) => (Equal(x.DeviceId, p1))), ), Quote((x, ) => (Equal(x.IndicatorId, p2))), ), Quote((item, ) => (String.op_Equality(Coalesce(NotEqual(item, NULLp3) ? item.Value : NULLp4, EmptyList).ToLower(), p6))), ), ))
12:04:40.125 [11] DEBUG NHibernate.Engine.Query.HQLQueryPlan - find: .Count[X4Data.Entity.IIndicatorsHistory](.Where[X4Data.Entity.IIndicatorsHistory](.Where[X4Data.Entity.IIndicatorsHistory](.Where[X4Data.Entity.IIndicatorsHistory](NHibernate.Linq.NhQueryable`1[X4Data.Entity.IIndicatorsHistory], Quote((x, ) => (Equal(x.DeviceId, p1))), ), Quote((x, ) => (Equal(x.IndicatorId, p2))), ), Quote((item, ) => (String.op_Equality(Coalesce(NotEqual(item, NULLp3) ? item.Value : NULLp4, EmptyList).ToLower(), p6))), ), )
12:04:40.125 [11] DEBUG NHibernate.Engine.QueryParameters - named parameters: {'p1'='72', 'p2'='1', 'p4'=null, 'p5'='', 'p6'='poweroff'}
12:04:40.126 [11] DEBUG NHibernate.AdoNet.AbstractBatcher - Opened new IDbCommand, open IDbCommands: 1
12:04:40.126 [11] DEBUG NHibernate.AdoNet.AbstractBatcher - Building an IDbCommand object for the SqlString: select cast(count(*) as NUMBER(10,0)) as col_0_0_ from WEBMONITOR.V_INDICATORS_HISTORY indicators0_ where indicators0_.DEVICE_ID=? and indicators0_.INDICATOR_ID=? and lower(nvl(cast(case when indicators0_.ID is not null or indicators0_.INDICATOR_ID is not null then indicators0_.VALUE else ? end as NVARCHAR2(255)), ?))=?
12:04:40.126 [11] DEBUG NHibernate.Type.Int64Type - binding '72' to parameter: 0
12:04:40.126 [11] DEBUG NHibernate.Type.Int64Type - binding '1' to parameter: 1
12:04:40.126 [11] DEBUG NHibernate.Type.StringType - binding '' to parameter: 3
12:04:40.126 [11] DEBUG NHibernate.Type.StringType - binding 'poweroff' to parameter: 4
12:04:40.126 [11] INFO  NHibernate.Loader.Loader - select cast(count(*) as NUMBER(10,0)) as col_0_0_ from WEBMONITOR.V_INDICATORS_HISTORY indicators0_ where indicators0_.DEVICE_ID=:p0 and indicators0_.INDICATOR_ID=:p1 and lower(nvl(cast(case when indicators0_.ID is not null or indicators0_.INDICATOR_ID is not null then indicators0_.VALUE else :p2 end as NVARCHAR2(255)), :p3))=:p4
12:04:40.126 [11] DEBUG NHibernate.SQL - select cast(count(*) as NUMBER(10,0)) as col_0_0_ from WEBMONITOR.V_INDICATORS_HISTORY indicators0_ where indicators0_.DEVICE_ID=:p0 and indicators0_.INDICATOR_ID=:p1 and lower(nvl(cast(case when indicators0_.ID is not null or indicators0_.INDICATOR_ID is not null then indicators0_.VALUE else :p2 end as NVARCHAR2(255)), :p3))=:p4;:p0 = 72 [Type: Int64 (0)], :p1 = 1 [Type: Int64 (0)], :p2 = NULL [Type: String (0)], :p3 = '' [Type: String (0)], :p4 = 'poweroff' [Type: String (8)]
12:04:40.126 [11] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
12:04:40.139 [11] ERROR NHibernate.AdoNet.AbstractBatcher - Could not execute query: select cast(count(*) as NUMBER(10,0)) as col_0_0_ from WEBMONITOR.V_INDICATORS_HISTORY indicators0_ where indicators0_.DEVICE_ID=:p0 and indicators0_.INDICATOR_ID=:p1 and lower(nvl(cast(case when indicators0_.ID is not null or indicators0_.INDICATOR_ID is not null then indicators0_.VALUE else :p2 end as NVARCHAR2(255)), :p3))=:p4
System.Data.OracleClient.OracleException (0x80131938): ORA-12704: character set mismatch

   at System.Data.OracleClient.OracleConnection.CheckError(OciErrorHandle errorHandle, Int32 rc)
   at System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, Boolean needRowid, OciRowidDescriptor& rowidDescriptor, ArrayList& resultParameterOrdinals)
   at System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, ArrayList& resultParameterOrdinals)
   at System.Data.OracleClient.OracleCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.OracleClient.OracleCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
   at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)

That mapping is that I use for bindings!

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="X4Data" namespace="X4Data.Entity" schema="WEBMONITOR">
  <class name="IndicatorsHistory" table="V_INDICATORS_HISTORY" lazy="true" >
    <composite-id>
      <key-property name="Id" column="ID" type="long"></key-property>
      <key-property name="IndicatorId" column="INDICATOR_ID" type="long"></key-property>
    </composite-id>

    <property name="Time" column="START_TIME" />
    <many-to-one class="CriticalState" lazy="false" name="CriticalState">
      <column name="CRITICAL_STATE_ID" />
    </many-to-one>
    <property name="DeviceId" column="DEVICE_ID" />
    <property name="Value" column="VALUE" />
    <property name="LocalizationKey" column="LOCALIZATION_KEY" />
    <property name="LocalizationValue" formula="(select GENERAL.RS_LOCALIZE(V_INDICATORS_HISTORY.LOCALIZATION_KEY, :CultureFilter.CultureId) FROM V_INDICATORS_HISTORY WHERE V_INDICATORS_HISTORY.INDICATOR_ID = INDICATOR_ID AND V_INDICATORS_HISTORY.ID = ID)" />
  </class>
</hibernate-mapping>

Mapping binding the presentation followed the format V_INDICATORS_HISTORY

SELECT
    uis.id,
    uis.indicator_id,
    ins.device_id,
    ins.start_time,
    uis.critical_state_id,
    uis.value,
    (SELECT localization_key
    FROM WEBMONITOR.indicator_value_codes ivc
    WHERE ivc.value = uis.value
    ) AS localization_key
FROM WEBMONITOR.integral_states ins,
    WEBMONITOR.unit_states unts,
    WEBMONITOR.unit_indicator_states uis
WHERE unts.integral_states_id = ins.id
AND uis.unit_state_id = unts.id

Help please this is very urgent, who should know how to fix the problem in NHibernate casts.

asp.net-mvc
linq
oracle
nhibernate
telerik
asked on Stack Overflow Jan 18, 2012 by Sergey Seletsky • edited Jan 18, 2012 by Sergey Seletsky

1 Answer

0

mapping as varchar is discussed here:

<property name="SomeName" />
    <column sql-type="varchar" />
</property>
answered on Stack Overflow Mar 25, 2012 by Alex • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0