NHibernateUpdate - индекс был вне диапазона. Должен быть неотрицательным и меньше размера коллекции - PullRequest
1 голос
/ 22 марта 2011

Исключение индекса вне диапазона поступает во время обновления. Код отлично работает при вставке, но не удается обновить Файл сопоставления

<class name="ReportView" lazy="false" table="GRIMIS_UI_OWNER.REPORT_VIEW">
    <id name="Id">
    <column name="REPORT_VIEW_ID" not-null="true" sql-type="number" />
    <generator class="sequence">
        <param  name="sequence">SEQ_REPORT_VIEW</param>
        <param  name="schema">GRIMIS_UI_OWNER</param>
    </generator>
    </id>
    <version column="CONCURRENCY_VERSION" name="Version" type="int"/>
    <property name="Name" column="REPORT_VIEW_NAME" />
    <property name="UserId" column="USER_ID" />
    <property name="Description" column="DESCRIPTION" />
    <property name="LastUpdatedBy" column="LAST_UPDATED_BY"/>
    <property name="LastUpdatedDate" column="LAST_UPDATED_DATE"/>
    <list name="Runs" table="REPORT_VIEW_RUN" cascade="all-delete-orphan" optimistic-lock="false">
    <key column="REPORT_VIEW_ID" />
    <index column="ORDER_ID" />
    <one-to-many class="ReportViewRun" />
     </list>
     <list name="Templates" table="REPORT_VIEW_TEMPLATE" >
    <key column="REPORT_VIEW_ID" />
    <index column="ORDER_ID" />
        <many-to-many class="Template" column="TEMPLATE_ID">
        </many-to-many>
      </list>
</class>

Код для обновления -

public void SaveReportView(ReportView reportView)
{
    using (ISession session = sessionManager.GetSession(SessionContextNames.CoreDatabase))
    {
         try
         {

                for (int i = 0; i < reportView.Templates.Count; i++)
                {
                       reportView.Templates[i] = session.Get<Template>(reportView.Templates[i].Id);
                       if (reportView.Templates[i] == null)
                       {
                            throw new TemplateNotFoundException("The Template referenced by this report view has been deleted.");
                       }
                  }
                  if (reportView.Id > 0)
                   {
                        //Load template
                        ReportView original = LoadViewRunsAndTemplatesById(reportView.Id);
                        if (original != null)
                        {
                            original.ApplyUpdates(reportView);

                            session.Update(original);
                        }
                        else
                        {
                            //Must have been deleted
                            reportView.ResetAllIds();
                            session.Save(reportView);
                        }
                    }
                    else
                    {
                        session.Save(reportView);
                    }
                    session.Flush();
                }
                catch (StaleObjectStateException ex)
                {
                    ReportView v;
                    try
                    {
                        v = session.Get<ReportView>(reportView.Id);
                    }
                    catch (ObjectNotFoundException)
                    {
                        //Must have been deleted between reading the original and trying to save (unlikely)
                        reportView.ResetAllIds();
                        session.Save(reportView);
                        return;
                    }

                    throw new ConcurrencyException(ErrorMessages.ReportViewHasBeenUpdated, ex, v.LastUpdatedBy, v.LastUpdatedDate.ToString());
                }

            }
      }



internal void ApplyUpdates(ReportView reportView)
{
            this.Name = reportView.Name;
            this.Description = reportView.Description;
            this.LastUpdatedBy = reportView.LastUpdatedBy;
            this.LastUpdatedDate = reportView.LastUpdatedDate;
            this.Version = reportView.Version;
            CollectionUtils.CopyList(reportView.Runs, this.Runs);
            int i =0;
            foreach(ReportViewRun r in reportView.Runs)
            { 
                r.Order = i;
                i = i + 1;
            }
           // this.Runs = (IList<ReportViewRun>)reportView.Runs;
           CollectionUtils.CopyList(reportView.Templates, this.Templates);
}



public static void CopyList<T>(IList<T> sourceList, IList<T> targetList)
{
            if (sourceList == null) throw new ArgumentNullException("sourceList");
            if (targetList == null) throw new ArgumentNullException("targetList");

            for (int i = 0; i < targetList.Count && i < sourceList.Count; i++)
            {
                targetList[i] = sourceList[i];
            }

            if (sourceList.Count > targetList.Count)
            {
                for (int i = targetList.Count; i <= sourceList.Count - 1; i++)
                {
                    targetList.Add(sourceList[i]);
                }
            }
            else if (sourceList.Count < targetList.Count)
            {
                for (int i = targetList.Count - 1; i >= sourceList.Count; i--)
                {
                    targetList.RemoveAt(i);
                }
            }
}

Пожалуйста, помогите, я застрял на 2 дня. Мне нужно разрешить как можно скорее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...