开发者

Problem with NHibernate:Unexpected row count: 0; expected: 1

I have two tables:

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

And classes:

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; }
    }

Mapping files:

<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开发者_如何学C 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>

Method to add record:

 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);
        }

I have error only for adding record, editing works well. Thanks.

EDIT: The Equals and GetHashCode methods is overriden.


I had the same problem with composite keys. It's related to the Equals() and GetHashCode().

You have to override those, otherwise NHibernate can't know the difference between them.

Have a look here and here for more information.

EDIT: It's actually related to the match between saved and unsaved objects. Missing overrides of Equals and GetHashCode is one of multiple thing which can cause this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜