Search code examples
c#asp.netnhibernate

Nhibernate System.IndexOutOfRangeException on not selected column


I am running into this Nhibernate related error that I have hard time debugging

I have the following class named OriginalDataSource

   public class OriginalDataSource
    {
        private string _OriginalSrc;
        private DateTime? _LastUpdtDate;
        private int _RecordsCount;
        private int _NegativeRecordsCount;
        private int _DataSourceId;
        private int _ProcId;
        //private int _ProcId;

    public virtual string OriginalSource
    {
        get { return _OriginalSrc; }
        set { _OriginalSrc = value; }
    }
    public virtual DateTime? LastUpdate
    {
        get { return _LastUpdtDate; }
        set { _LastUpdtDate = value; }
    }
    public virtual int RecordsCount
    {
        get { return _RecordsCount; }
        set { _RecordsCount = value; }
    }
    public virtual int NegativeRecordsCount
    {
        get { return _NegativeRecordsCount; }
        set { _NegativeRecordsCount = value; }
    }
    public virtual int DataSourceId
    {
        get { return _DataSourceId; }
        set { _DataSourceId = value; }
    }
    public virtual int ProcId
    {       
        get { return _ProcId; }
        set { _ProcId = value; }
    }
}

with the following Nhibernate Mapping

<composite-id>
  <key-property name="ProcId" column="PROC_ID" access="field.pascalcase-underscore"/>
  <key-property name="LastUpdtDate" type="DateTime" column="MAX_LAST_UPDT_DATE" access="field.pascalcase-underscore" />
</composite-id>
<property name="OriginalSrc" column="LAST_UPDT_SRC" access="field.pascalcase-underscore" />
<property name="DataSourceId" column="DATASOURCE_ID" access="field.pascalcase-underscore" />
<property name="RecordsCount" column="CNT" access="field.pascalcase-underscore" />
<property name="NegativeRecordsCount" column="CNT_NEG" access="field.pascalcase-underscore" />

This entity has a many-to-one relationship referenced by the foreign key Proc_ID from DataSourceProc.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="save-update" auto-import="true">
    <class name="EFTS.Common.Entities.InternalDashboard.DataSourceProc, EFTS.Common" lazy="true" table="DATASOURCE_PROCS">
    
    //Rest of the mapping, may not be relevant for this question
    
    <set name="OriginalDataSrcs" lazy="true" cascade="none">
      <key column="PROC_ID" />
      <one-to-many class="EFTS.Common.Entities.InternalDashboard.OriginalDataSource, EFTS.Common" />
    </set>
    
    </class>
</hibernate-mapping>

When I tried to run the following query:

    string countQuery = @"
        Select proc_id, datasource_id, last_updt_src, Coalesce(Sum(cnt), 0) cnt, Max(max_last_updt_date) max_last_updt_date
        From EFTS.My_Table
        Where To_Date(max_last_updt_date, 'yyyy/MM/dd')
            Between To_Date(:StartDate, 'yyyy/MM/dd') And To_Date(:EndDate, 'yyyy/MM/dd')
        Group By proc_id, datasource_id, last_updt_src";

        IQuery query = Uow.CreateSQLQuery(countQuery)
            .AddEntity(typeof(OriginalDataSource))
            .SetParameter("StartDate", dateForm.StartDate.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture))
            .SetParameter("EndDate", dateForm.EndDate.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture));

        var res = query.List<OriginalDataSource>();

I received a 'could not execute query' with inner exception System.IndexOutOfRangeException on one of the column that is not even selected:

System.IndexOutOfRangeException:
CNT_NEG

However, if I get rid of .AddEntity(typeof(OriginalDataSource)) from the criteria and do a generic query.List(), I am actually getting result back.

What I need help with:

I am trying to figure out why I am getting this error. Is there anything wrong with my mapping? I have read several SO posts, one of them being this. However, I checked for double mapping, as well as tried to make _NegativeRecordsCount nullable, but still getting the same error. I believe my case is unique to other similar posts.

Additional Information:

Nhibernate Version 2.1


Solution

  • Because that column is included in the mapping, you must include it in your SQL string, if you use it or not.

    You can fudge it if you are sure you are not using it by setting the value in the select statement: select CNT_NEG = 0, etc...