使类的属性值脱水时出错

时间:2012-11-24 07:32:24

标签: c# nhibernate

我正在尝试插入一个对象,但它让我感到Error: dehydrating property value for FrancosPoS.DBMapping.order.obs

我在这里寻找

NHibernate: Error dehydrating property - What the heck is this?

NHibernate - Error dehydrating property value

Receiving Index Out of Range with NHibernate, 但他们都没有工作。

我检查并重新检查了我的映射到多列声明,但没有发现任何问题。

我的线索是问题与MySQL数据库和identity generator上的bigint有关。当我将其更改为increment时,脱水错误消失,并出现Index out of range异常。

此外,Session Save电话会发生这种情况。使用increment生成器时,错误发生在commit时间。

这是我的订单xml映射:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="order" table="`order`" lazy="true" >
<id name="idOrder">
  <generator class="identity" />
</id>
<many-to-one lazy="false" name="employee">
  <column name="idEmployee" sql-type="bigint(20)" not-null="false" />
</many-to-one>
<many-to-one lazy="false" name="customer">
  <column name="idCustomer" sql-type="bigint(20)" not-null="false" />
</many-to-one>
<many-to-one lazy="false" name="address">
  <column name="idAddress" sql-type="bigint(20)" not-null="false" />
</many-to-one>
<property name="date">
  <column name="date" sql-type="datetime" not-null="true" />
</property>
<property name="bakingTime">
  <column name="date" sql-type="datetime" not-null="false" />
</property>
<property name="price">
  <column name="price" sql-type="decimal(8,4)" not-null="true" />
</property>
<property name="cash">
  <column name="cash" sql-type="tinyint(1)" not-null="false" />
</property>
<property name="credit">
  <column name="credit" sql-type="tinyint(1)" not-null="false" />
</property>
<property name="houseNumber">
  <column name="houseNumber" sql-type="bigint(20)" not-null="false" />
</property>
<property name="complement">
  <column name="complement" sql-type="bigint(20)" not-null="false" />
</property>
<property name="obs">
  <column name="obs" sql-type="varchar(350)" not-null="false" />
</property>

<bag name="orderPsi" table="ordPsi" cascade="all" inverse="true">
      <key column="idOrdPastaI" />
      <one-to-many class="ordPsi" />
</bag>
</class>
</hibernate-mapping>

和cs课:

public partial class order {
    public order() { }
    public virtual long idOrder { get; set; }
    public virtual employee employee { get; set; }
    public virtual customer customer { get; set; }
    public virtual address address { get; set; }
    public virtual System.DateTime date { get; set; }
    public virtual System.Nullable<System.DateTime> bakingTime { get; set; }
    public virtual string price { get; set; }
    public virtual System.Nullable<int> cash { get; set; }
    public virtual System.Nullable<int> credit { get; set; }
    public virtual System.Nullable<long> houseNumber { get; set; }
    public virtual System.Nullable<long> complement { get; set; }
    public virtual string obs { get; set; }
    public virtual IList<ordPsi> orderPsi { get; set; }
}

这是堆栈跟踪后面的内部异常:

"Parameter index is out of range."
at MySql.Data.MySqlClient.MySqlParameterCollection.CheckIndex(Int32 index)
at MySql.Data.MySqlClient.MySqlParameterCollection.GetParameter(Int32 index)
at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, 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)

2 个答案:

答案 0 :(得分:3)

我通过反复试验找到了解决方案,对xml文件中的所有映射进行了注释,这些映射在数据库中可能为null,而且我被卡在bakingTime属性中:

<property name="bakingTime">
  <column name="date" sql-type="datetime" not-null="false" />
</property>

正如我猜测的那样(以及其他人在其他帖子上提出的建议),映射是错误的。通过复制粘贴问题[:P],bakingTime列名称引用date列,而不是自身!!!

所以,这应该是:

<property name="bakingTime">
  <column name="bakingTime" sql-type="datetime" not-null="false" />
</property>

此外,这篇文章仍然很有价值,尽管不是那么新:

"NHibernate Nullable DateTime Issues - Ayende"

答案 1 :(得分:1)

.Net代码的有趣命名方案,但您不应该同时使用&lt; id&gt;和&lt; property&gt;对于idOrder。