NHibernate的问题:意外的行数:0;预期:1

时间:2011-08-10 11:54:46

标签: c# nhibernate

我有两张桌子:

DIC_STATUS:

STATUS_ID

   1
   2

DIC_STATUS_STRINGS:

STATUS_ID   LANG_ID   STATUS_NAME

    1         1         Name1
    1         2         Name1_1
    2         1         Name2
    2         2         Name2_2

课程:

public class StatusS : AbstractDictionaryObject
    {
        public virtual string StatusName { get; set; }
        public virtual Languages Lang { get; set; }
        public virtual Status Status { get; set; }
    }

  public class Status:AbstractDisplayDictionary
    {
        [Browsable(false)]
        public IList<StatusS> DicStatusStrings { get; set; }
        public Status()
        {
            DicStatusStrings = new List<StatusS>();
        }
    }

 public abstract class AbstractDisplayDictionary
    {
        [Browsable(false)]
        public virtual int Id { get; set; }
        [DisplayName("Наименование на русском")]
        public virtual string NameRu { get; set; }
        [DisplayName("Наименование на казахском")]
        public virtual string NameKz { get; set; }
    }

映射文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="EArchive.Data" namespace="EArchive.Data.Class">  
  <class name="Status" table="DIC_STATUS" lazy="false">
    <id name="Id" column="STATUS_ID">
      <generator class="increment">
      </generator>
    </id>
    <bag name="DicStatusStrings" inverse="true" cascade="all-delete-orphan">
      <key column="STATUS_ID" />
      <one-to-many class="StatusS" />
    </bag>
    <property name="NameRu"
              formula="(SELECT str.STATUS_NAME FROM DIC_STATUS_STRINGS str
              WHERE str.Status_Id = Status_Id and str.Lang_Id=1)"/>

    <property name="NameKz"
              formula="(SELECT str.STATUS_NAME FROM DIC_STATUS_STRINGS str
              WHERE str.Status_Id = Status_Id and str.Lang_Id=2)"/>    
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="EArchive.Data" namespace="EArchive.Data.Class">
<class name="StatusS" table="DIC_STATUS_STRINGS" lazy="false">
  <composite-id>    
    <key-many-to-one name="Status" class="Status">
      <column name="STATUS_ID"/>
    </key-many-to-one>
    <key-property name="LangId" column="LANG_ID"/>
  </composite-id>
  <property name="StatusName" column="STATUS_NAME"/>
  <many-to-one name="Lang" class="Languages" column="LANG_ID" />  
</class>
</hibernate-mapping>

添加记录的方法:

 public void AddRecord(object record)
        {
            StatusDAO statusDao = GetStatusDAO();
            var status = (Status) record;
            IList<StatusS> statusS = status.DicStatusStrings;
            status.DicStatusStrings = null;
            status = statusDao.Save(status);

            foreach (var s in statusS)
            {
                s.Status = status;
            }
            status.DicStatusStrings = statusS;
            statusDao.Update(status);
        }

我只有添加记录的错误,编辑效果很好。感谢。

编辑:重写Equals和GetHashCode方法。

1 个答案:

答案 0 :(得分:1)

我对复合键有同样的问题。它与Equals()和GetHashCode()有关。

你必须override those,否则NHibernate无法知道它们之间的区别。

有关详细信息,请查看herehere

编辑:它实际上与已保存和未保存对象之间的匹配有关。缺少Equals和GetHashCode的覆盖是导致这种情况的多重因素之一。