【问题标题】:Join table in HNibernate mapping issue在 Hibernate 映射问题中加入表
【发布时间】:2014-07-24 12:25:36
【问题描述】:

我的 .hbm.xml 文件中有第二个代码:

  <class name="Option" table="tOptions">
    <id name="Id">
      <column name="Id" sql-type="int"/>
      <generator class="native"/>
    </id>
    <property name="DocumentId" column="Ref"/>
    <property name="QuoteRef"/>
    <property name="OptionCode" column="OpCode"/>
    <property name="Description" column="Description"/>
    <property name="SupplierID" column="SupplierID"/>
    <property name="OptionTypeId" column="OptionType"/>
    <join table="tActiveOffers">
      <key column="Ref" unique="true"/>
      <property name="Colour" column="ExteriorColor"/>
      <property name="Trim" column="InteriorColor"/>
    </join>
  </class>

看起来很正常,但是当我尝试从数据库中获取某些内容时,我收到了一个错误: “此 sqlparametercollection 的索引 11 无效,count=11”,在大多数情况下,这指向 NHibernate 问题。此外,在将 2 个新字段(颜色和修剪)添加到 Option 类之前,一切正常。所以我认为问题在于不正确的连接。

如果键列等于 Id,它将正常工作

<join table="tActiveOffers">
  <key column="Id" unique="true"/>
  <property name="Colour" column="ExteriorColor"/>
  <property name="Trim" column="InteriorColor"/>
</join>

但这对我不利(ID 与两张表不同)。

我猜是密钥不正确或其他问题。

我缺少什么以及如何更正确地完成它?

编辑: 错误堆栈跟踪:

   at System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 index)
   at System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32 index)
   at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
   at NHibernate.Type.Int32Type.Set(IDbCommand rs, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
   at NHibernate.Action.EntityUpdateAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
   at NHibernate.Event.Default.DefaultAutoFlushEventListener.OnAutoFlush(AutoFlushEvent event)
   at NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet`1 querySpaces)
   at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
   at NHibernate.Impl.ExpressionQueryImpl.List()
   at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

【问题讨论】:

    标签: c# sql nhibernate


    【解决方案1】:

    如果key列的目标不是id,而是某个属性,我们必须使用property-ref,所以不要这样:&lt;key column="Ref" unique="true"/&gt;使用这个

    <join table="tActiveOffers">
      <key column="Ref" unique="true" property-ref="DocumentId"/>
      <property name="Colour" column="ExteriorColor"/>
      <property name="Trim" column="InteriorColor"/>
    </join>
    

    【讨论】:

    • 谢谢。可悲的是,我仍然收到这个奇怪的“这个 sqlparametercollection 的无效索引 11,count=11”,但至少我的映射现在很好。
    • 这个异常是在插入过程中。对吗?你确定你的 DocumentId 真的有一些价值吗?还有,你的映射 sn-p 是 1:1 吗?
    • 嗯,不。当我尝试从数据库中获取值时会出现此异常。 DocumentId 总是有值。抱歉,没明白 sn-p 1:1 是什么意思...
    • 嗯,完全相同的场景对我有用,所以我想问你,用完整的堆栈扩展你的答案,请......这会有所帮助
    • 我真的不明白你。全栈什么?无论如何,我将我的加入放入 标记中,一切正常,所以我猜这个错误肯定是在 Nhibernate 中的某个地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2016-08-07
    相关资源
    最近更新 更多