概述:
当用Nhibernate操作主从表时,往主表插入数据时,当执行到提交事务时出现如下错误:
此 OracleParameterCollection 的 Count=4 的索引 4 无效。
在 System.Data.OracleClient.OracleParameterCollection.RangeCheck(Int32 index) 在 System.Data.OracleClient.OracleParameterCollection.GetParameter(Int32 index) 在 System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index) 在 NHibernate.Type.DecimalType.Set(IDbCommand st, Object value, Int32 index) 在 NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) 在 NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session) 在 NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) 在 NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) 在 NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) 在 NHibernate.Action.EntityInsertAction.Execute() 在 NHibernate.Engine.ActionQueue.Execute(IExecutable executable) 在 NHibernate.Engine.ActionQueue.ExecuteActions(IList list) 在 NHibernate.Engine.ActionQueue.ExecuteActions() 在 NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 在 NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) 在 NHibernate.Impl.SessionImpl.Flush() 在 NHibernate.Transaction.AdoTransaction.Commit() 在 DataAccess.DictateDao.AddRecord(String registerOrgid, String registerOperatorid, String dictateTitle, String dictateContent, String dictateType, String[] informOrgid, String[] accessoryName, String[] accessorySize) 位置 D:\OA\DataAccess\DataAccess\DictateDao.cs:行号 64
主表xml如下:
相应的CS如下:
using MyGeneration/Template/NHibernate (c) by Sharp 1.4
based on OHM (alvy77@hotmail.com)*/using System;using System.Collections;using System.Collections.Generic;using Iesi.Collections;namespace Model
{/// <summary>/// IJkptOaxtDictate interface for NHibernate mapped table 'JKPT_OAXT_DICTATE'./// </summary> public interface IJkptOaxtDictate
{#region Public Propertiesdecimal Dictateid
{get ;set ;
}decimal Registerorgid
{get ;set ;
}string Registeroperatorid
{get ;set ;
}
DateTime Registertime
{get ;set ;
}string Dictatetitle
{get ;set ;
}string Dictatedesc
{get ;set ;
}decimal Modifyorgid
{get ;set ;
}string Modifyoperatorid
{get ;set ;
}
DateTime Modifytime
{get ;set ;
}decimal Isbrowse
{get ;set ;
}decimal Browseorgid
{get ;set ;
}string Browseoperatorid
{get ;set ;
}
DateTime Browsetime
{get ;set ;
}decimal Informtype
{get ;set ;
}
ISet details
{get;set;
}bool IsDeleted { get; set; }bool IsChanged { get; set; }#endregion
}/// <summary>/// JkptOaxtDictate object for NHibernate mapped table 'JKPT_OAXT_DICTATE'./// </summary> [Serializable]public class JkptOaxtDictate : ICloneable,IJkptOaxtDictate
{#region Member Variablesprotected decimal _dictateid;protected decimal _registerorgid;protected string _registeroperatorid;protected DateTime _registertime;protected string _dictatetitle;protected string _dictatedesc;protected decimal _modifyorgid;protected string _modifyoperatorid;protected DateTime _modifytime;protected decimal _isbrowse;protected decimal _browseorgid;protected string _browseoperatorid;protected DateTime _browsetime;protected decimal _informtype;private ISet _details=new HashedSet();protected bool _bIsDeleted;protected bool _bIsChanged;#endregion#region Constructorspublic JkptOaxtDictate() {}public JkptOaxtDictate(decimal pDictateid, decimal pRegisterorgid, string pRegisteroperatorid, DateTime pRegistertime, string pDictatetitle, string pDictatedesc, decimal pModifyorgid, string pModifyoperatorid, DateTime pModifytime, decimal pIsbrowse, decimal pBrowseorgid, string pBrowseoperatorid, DateTime pBrowsetime, decimal pInformtype)
{this._dictateid = pDictateid;this._registerorgid = pRegisterorgid;this._registeroperatorid = pRegisteroperatorid;this._registertime = pRegistertime;this._dictatetitle = pDictatetitle;this._dictatedesc = pDictatedesc;this._modifyorgid = pModifyorgid;this._modifyoperatorid = pModifyoperatorid;this._modifytime = pModifytime;this._isbrowse = pIsbrowse;this._browseorgid = pBrowseorgid;this._browseoperatorid = pBrowseoperatorid;this._browsetime = pBrowsetime;this._informtype = pInformtype;
}public JkptOaxtDictate(decimal pDictateid)
{this._dictateid = pDictateid;
}#endregion#region Public Propertiespublic decimal Dictateid
{get { return _dictateid; }set { _bIsChanged |= (_dictateid != value); _dictateid = value; }
}public decimal Registerorgid
{get { return _registerorgid; }set { _bIsChanged |= (_registerorgid != value); _registerorgid = value; }
}public string Registeroperatorid
{get { return _registeroperatorid; }set
{if (value != null && value.Length > 6)throw new ArgumentOutOfRangeException("Registeroperatorid", "Registeroperatorid value, cannot contain more than 6 characters");
_bIsChanged |= (_registeroperatorid != value);
_registeroperatorid = value;
}
}public DateTime Registertime
{get { return _registertime; }set { _bIsChanged |= (_registertime != value); _registertime = value; }
}public string Dictatetitle
{get { return _dictatetitle; }set
{if (value != null && value.Length > 200)throw new ArgumentOutOfRangeException("Dictatetitle", "Dictatetitle value, cannot contain more than 200 characters");
_bIsChanged |= (_dictatetitle != value);
_dictatetitle = value;
}
}public string Dictatedesc
{get { return _dictatedesc; }set
{if (value != null && value.Length > 4000)throw new ArgumentOutOfRangeException("Dictatedesc", "Dictatedesc value, cannot contain more than 4000 characters");
_bIsChanged |= (_dictatedesc != value);
_dictatedesc = value;
}
}public decimal Modifyorgid
{get { return _modifyorgid; }set { _bIsChanged |= (_modifyorgid != value); _modifyorgid = value; }
}public string Modifyoperatorid
{get { return _modifyoperatorid; }set
{if (value != null && value.Length > 6)throw new ArgumentOutOfRangeException("Modifyoperatorid", "Modifyoperatorid value, cannot contain more than 6 characters");
_bIsChanged |= (_modifyoperatorid != value);
_modifyoperatorid = value;
}
}public DateTime Modifytime
{get { return _modifytime; }set { _bIsChanged |= (_modifytime != value); _modifytime = value; }
}public decimal Isbrowse
{get { return _isbrowse; }set { _bIsChanged |= (_isbrowse != value); _isbrowse = value; }
}public decimal Browseorgid
{get { return _browseorgid; }set { _bIsChanged |= (_browseorgid != value); _browseorgid = value; }
}public string Browseoperatorid
{get { return _browseoperatorid; }set
{if (value != null && value.Length > 6)throw new ArgumentOutOfRangeException("Browseoperatorid", "Browseoperatorid value, cannot contain more than 6 characters");
_bIsChanged |= (_browseoperatorid != value);
_browseoperatorid = value;
}
}public DateTime Browsetime
{get { return _browsetime; }set { _bIsChanged |= (_browsetime != value); _browsetime = value; }
}public decimal Informtype
{get { return _informtype; }set { _bIsChanged |= (_informtype != value); _informtype = value; }
}public ISet details
{get { return _details; }set { this._details = value; }
}public bool IsDeleted
{get
{return _bIsDeleted;
}set
{
_bIsDeleted = value;
}
}public bool IsChanged
{get
{return _bIsChanged;
}set
{
_bIsChanged = value;
}
}#endregion #region ICloneable methodspublic object Clone()
{return this.MemberwiseClone();
}#endregion
}#region Custom ICollection interface for JkptOaxtDictate public interface IJkptOaxtDictateCollection : ICollection
{
JkptOaxtDictate this[int index]{ get; set; }void Add(JkptOaxtDictate pJkptOaxtDictate);void Clear();
}
[Serializable]public class JkptOaxtDictateCollection : IJkptOaxtDictateCollection
{private IList<JkptOaxtDictate> _arrayInternal;public JkptOaxtDictateCollection()
{
_arrayInternal = new List<JkptOaxtDictate>();
}public JkptOaxtDictateCollection( IList<JkptOaxtDictate> pSource )
{
_arrayInternal = pSource;if(_arrayInternal == null)
{
_arrayInternal = new List<JkptOaxtDictate>();
}
}public JkptOaxtDictate this[int index]
{get
{return _arrayInternal[index];
}set
{
_arrayInternal[index] = value;
}
}public int Count { get { return _arrayInternal.Count; } }public bool IsSynchronized { get { return false; } }public object SyncRoot { get { return _arrayInternal; } }public void CopyTo(Array array, int index){ _arrayInternal.CopyTo((JkptOaxtDictate[])array, index); }public IEnumerator GetEnumerator() { return _arrayInternal.GetEnumerator(); }public void Add(JkptOaxtDictate pJkptOaxtDictate) { _arrayInternal.Add(pJkptOaxtDictate); }public void Clear() { _arrayInternal.Clear(); }public IList<JkptOaxtDictate> GetList() { return _arrayInternal; }
}#endregion
}
从表xml如下:
相应的CS如下:
using MyGeneration/Template/NHibernate (c) by Sharp 1.4
based on OHM (alvy77@hotmail.com)*/using System;using System.Collections;using System.Collections.Generic;namespace Model
{/// <summary>/// IJkptOaxtDictatedetails interface for NHibernate mapped table 'JKPT_OAXT_DICTATEDETAILS'./// </summary> public interface IJkptOaxtDictatedetails
{#region Public Propertiesdecimal Dictatedetailsid
{get ;set ;
}decimal Dictateid
{get ;set ;
}decimal Targetorgid
{get ;set ;
}decimal Sign
{get ;set ;
}
JkptOaxtDictate dictate
{get;set;
}bool IsDeleted { get; set; }bool IsChanged { get; set; }#endregion
}/// <summary>/// JkptOaxtDictatedetails object for NHibernate mapped table 'JKPT_OAXT_DICTATEDETAILS'./// </summary> [Serializable]public class JkptOaxtDictatedetails : ICloneable,IJkptOaxtDictatedetails
{#region Member Variablesprotected decimal _dictatedetailsid;protected decimal _dictateid;protected decimal _targetorgid;protected decimal _sign;protected JkptOaxtDictate _dictate;protected bool _bIsDeleted;protected bool _bIsChanged;#endregion#region Constructorspublic JkptOaxtDictatedetails() {}public JkptOaxtDictatedetails(decimal pDictatedetailsid, decimal pDictateid, decimal pTargetorgid, decimal pSign)
{this._dictatedetailsid = pDictatedetailsid; this._dictateid = pDictateid; this._targetorgid = pTargetorgid; this._sign = pSign;
}public JkptOaxtDictatedetails(decimal pDictatedetailsid)
{this._dictatedetailsid = pDictatedetailsid;
}#endregion#region Public Propertiespublic decimal Dictatedetailsid
{get { return _dictatedetailsid; }set { _bIsChanged |= (_dictatedetailsid != value); _dictatedetailsid = value; }
}public decimal Dictateid
{get { return _dictateid; }set { _bIsChanged |= (_dictateid != value); _dictateid = value; }
}public decimal Targetorgid
{get { return _targetorgid; }set { _bIsChanged |= (_targetorgid != value); _targetorgid = value; }
}public decimal Sign
{get { return _sign; }set { _bIsChanged |= (_sign != value); _sign = value; }
}public JkptOaxtDictate dictate
{get {return _dictate;
}set { this._dictate = value; }
}public bool IsDeleted
{get
{return _bIsDeleted;
}set
{
_bIsDeleted = value;
}
}public bool IsChanged
{get
{return _bIsChanged;
}set
{
_bIsChanged = value;
}
}#endregion #region ICloneable methodspublic object Clone()
{return this.MemberwiseClone();
}#endregion
}#region Custom ICollection interface for JkptOaxtDictatedetails public interface IJkptOaxtDictatedetailsCollection : ICollection
{
JkptOaxtDictatedetails this[int index]{ get; set; }void Add(JkptOaxtDictatedetails pJkptOaxtDictatedetails);void Clear();
}
[Serializable]public class JkptOaxtDictatedetailsCollection : IJkptOaxtDictatedetailsCollection
{private IList<JkptOaxtDictatedetails> _arrayInternal;public JkptOaxtDictatedetailsCollection()
{
_arrayInternal = new List<JkptOaxtDictatedetails>();
}public JkptOaxtDictatedetailsCollection( IList<JkptOaxtDictatedetails> pSource )
{
_arrayInternal = pSource;if(_arrayInternal == null)
{
_arrayInternal = new List<JkptOaxtDictatedetails>();
}
}public JkptOaxtDictatedetails this[int index]
{get
{return _arrayInternal[index];
}set
{
_arrayInternal[index] = value;
}
}public int Count { get { return _arrayInternal.Count; } }public bool IsSynchronized { get { return false; } }public object SyncRoot { get { return _arrayInternal; } }public void CopyTo(Array array, int index){ _arrayInternal.CopyTo((JkptOaxtDictatedetails[])array, index); }public IEnumerator GetEnumerator() { return _arrayInternal.GetEnumerator(); }public void Add(JkptOaxtDictatedetails pJkptOaxtDictatedetails) { _arrayInternal.Add(pJkptOaxtDictatedetails); }public void Clear() { _arrayInternal.Clear(); }public IList<JkptOaxtDictatedetails> GetList() { return _arrayInternal; }
}#endregion
}
主程序如下:
{bool IsSuccess = false;string dictateID ="";
JkptOaxtDictate od = new JkptOaxtDictate();
JkptOaxtDictatedetails detail = new JkptOaxtDictatedetails();
ITransaction trans = session.BeginTransaction();try
{
od.Registerorgid = decimal.Parse(registerOrgid);
od.Registeroperatorid = registerOperatorid;
od.Registertime = DateTime.Now.ToLocalTime();
od.Dictatetitle = dictateTitle;
od.Dictatedesc = dictateContent;
od.Informtype = decimal.Parse(dictateType);
od.Modifytime = DateTime.Now.ToLocalTime();
od.Browsetime = DateTime.Now.ToLocalTime();for (int i = 0; i < informOrgid.Length; i++)
{
detail.Dictateid = decimal.Parse("3");
detail.Targetorgid = decimal.Parse(informOrgid[i]);
detail.Sign = 0;
detail.dictate = od;
od.details.Add(detail);
session.Save(od);
}
trans.Commit(); //写日志 Helpers.SaveInfo("Insert dictate 's infomation Success!");
IsSuccess = true;
}catch (Exception ex)
{
IsSuccess = false;
trans.Rollback();//写日志 Helpers.SaveInfo("插入失败!错误提示如下:" + ex.Message);
}finally
{if (session != null)
{
session.Clear();
}
}return IsSuccess;
}
当执行到trans.Commit()时出现如上所述错误.
解决办法:
原因是:
从表关联主表时的问题. <many-to-one name="dictate" column="DICTATEID" class="Model.JkptOaxtDictate,Model"/>
即添加属性insert="false" update="false"