1

私はNHibernateに少し慣れていません。C# を使用して asp.net を使用して Web アプリケーションを実装したいと考えています。

次のデータベース スキーマがあります。

データベース スキーマ

これが私の NHibernate マッピング ファイルです。マッピングが正しいかどうかわかりません。間違っていたら訂正してください。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Status, TelDir.Core" table="tblStatus" lazy="false">
    <id name="ID" column="StatusID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="StatusCode" column="StatusCode" />
    <property name="StatusName" column="StatusName" />

    <!--
    <set name="WorkOrderStatus" table="tblWorkOrderStatus" inverse="true">
      <key column="StatusID" />
      <one-to-many class="TelDir.Core.Domain.WorkOrderStatus, TelDir.Core" />
    </set>
    -->
  </class>

</hibernate-mapping>



<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.WorkOrder, TelDir.Core" table="tblWorkOrder" lazy="false">
    <id name="ID" column="WOID" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="WorkOrderRef" column="WORef" />
    <property name="WorkOrderDesc" column="WODesc" />


    <set name="WorkOrderStatus" table="tblWorkOrderStatus" inverse="true">
      <key column="WOID" />
      <one-to-many class="TelDir.Core.Domain.WorkOrderStatus, TelDir.Core" />
    </set>

  </class>
</hibernate-mapping>



<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.WorkOrderStatus, TelDir.Core" table="tblWorkOrderStatus" lazy="false">
    <composite-id>
      <key-many-to-one name="WorkOrder" column="WOID"/>
      <key-many-to-one name="Status" column="StatusID"/>
    </composite-id>
    <property name="LastModifyDateTime" column="LastModifiedOn"  type="Timestamp" />
    <property name="CreatedBy" column="CreatedBy" />
  </class>
</hibernate-mapping>  

そして、私のPOCOクラスは以下のように提示されます

public class Status : DomainObject<Int16>
    {
        private string _statuscode = "";
        private string _statusname = "";
        //private ISet<WorkOrderStatus> _workorder_status = new HashedSet<WorkOrderStatus>()  ;

        public Status() { }

        public Status(string statusCode, string statusName) {
            this._statuscode = statusCode;
            this._statusname = statusName; 
        }

        public string StatusCode        {
            get { return _statuscode ; }
            set { _statuscode  = value; }
        }

        public string StatusName
        {
            get { return _statusname; }
            set { _statusname = value; }
        }

        /*
        public ISet<WorkOrderStatus> WorkOrderStatus
        {
            get { return (_workorder_status); }
            protected set { _workorder_status = value; }
        }
        */
    }



  public class WorkOrder : DomainObject<long>
    {
        private string _workorder_ref = "";
        private string _workorder_desc = "";
        private ISet<WorkOrderStatus> _workorder_status = new HashedSet<WorkOrderStatus>();

        public WorkOrder() { }

        public WorkOrder(string wref, string wdecs) {
            this._workorder_ref = wref;
            this._workorder_desc = wdecs;
        }

        public string WorkOrderRef   {
            get { return _workorder_ref ; }
            set { _workorder_ref = value; }
        }

        public string WorkOrderDesc
        {
            get { return _workorder_desc; }
            set { _workorder_desc = value; }
        }


        public ISet<WorkOrderStatus> WorkOrderStatus
        {
            get { return (_workorder_status); }
            protected set { _workorder_status = value; }
        }


        public void AddStatus(Status st, DateTime dt)
        {
                WorkOrderStatus obj = new WorkOrderStatus();
                obj.WorkOrder = this;
                obj.Status = st;
                obj.LastModifyDateTime = dt;
                _workorder_status.Add(obj);
        }
    }


    public class WorkOrderStatus 
    {
        private DateTime _lastmodifydt;
        private WorkOrder _workorder;
        private Status _status;
        private int _createdby;

        public WorkOrderStatus() { 
        }



        public DateTime LastModifyDateTime{ 
            get { return _lastmodifydt; }
            set { _lastmodifydt = value; }
        }

        public WorkOrder WorkOrder
        {
            get { return _workorder; }
            set { _workorder = value; }
        }
        public Status  Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public int CreatedBy {
            get { return _createdby; }
            set { _createdby = value; }
        }


        public override bool Equals(object other)
        {

            //if (this == other) return true;

            //WorkOrderStatus obj = other as WorkOrderStatus;
            //if (obj == null) return false; // null or not a cat

            //if (_lastmodifydt != obj._lastmodifydt ) return false;            

            //return true;


            if (other == null)
                return false;
            WorkOrderStatus t = other as WorkOrderStatus;
            if (t == null)
                return false;
            if (WorkOrder  == t.WorkOrder  && Status  == t.Status && _lastmodifydt == t.LastModifyDateTime )
                return true;
            return false;  
        }

        public override int GetHashCode()
        {

            unchecked
            {
                int result;
                result = _lastmodifydt.GetHashCode();
                result = 29 * result + WorkOrder.GetHashCode() + Status.GetHashCode();
                return result;
            }

            //return (WorkOrder.ID + "|" + Status.ID + "|" + Status.StatusName).GetHashCode();  
        }

    }

次のようなテーブルにデータを表示したい:

[tblWorkOrderStatus]

 StatusID           WOID              LastModifiedOn              CreatedBy
 --------------------------------------------------------------------------
 2                  1                 06/20/2012 09:45:40.209         1  

[tblWorkOrder]

 WOID               WORef             WODesc
 -------------------------------------------
   1                001               Test-001 

[tblステータス]

 StatusID           StatusCode        StatusName
 -----------------------------------------------
   1                'X001'            OPEN
   2                'X002'            CLOSE

[tblWorkOrderStatus] にレコードを追加するにはどうすればよいですか?

以下のようにテストコードを書きましたが、関連付けテーブル [tblWorkOrderStatus] に追加されたレコードが見つかりませんでした。追加されない理由がわかりません。

           WorkOrder Wo = new WorkOrder('001', 'Test-001');
           daoFactory.GetWorkOrderDao().Save(Wo);

           Status St = daoFactory.GetStatusDao().GetById(1, false);

                //// Secode Methode
                WorkOrderStatus _objWS = new WorkOrderStatus();
                _objWS.WorkOrder      = Wo;
                _objWS.Status     = St;
                _objWS.LastModifyDateTime = DateTime.Now;
                _objWS.CreatedBy = 1; //suppose 1 is current login UserID 

                Wo.WorkOrderStatus.Add(_objWS);

          daoFactory.GetWorkOrderDao().Save(Wo);

POCO、NHibernate マッピング ファイル、またはその他の場所で何かが不足している可能性があります。正しい解決策を教えてください。

よろしくお願いします、

ここに私のスタックトレースがあります:

"   at System.ThrowHelper.ThrowKeyNotFoundException()\r\n   
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)\r\n   
at NHibernate.Engine.StatefulPersistenceContext.RemoveEntity(EntityKey key)\r\n   
at NHibernate.Action.EntityDeleteAction.Execute()\r\n   
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)\r\n   
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)\r\n   
at NHibernate.Engine.ActionQueue.ExecuteActions()\r\n   
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)\r\n   
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)\r\n   
at NHibernate.Impl.SessionImpl.Flush()\r\n   
at NHibernate.Transaction.AdoTransaction.Commit()\r\n   
at TelDir.Data.NHibernateSessionManager.CommitTransaction() 
in E:\\OLD PC\\D\\WORKS\\PROJECT\\TelDIR\\Data\\NHibernateSessionManager.cs:line 120\r\n   
at TelDir.Web.NHibernateSessionModule.CommitAndCloseSession(Object sender, EventArgs e) 
in e:\\OLD PC\\D\\WORKS\\PROJECT\\TelDIR\\Web\\App_Code\\NHibernateSessionModule.cs:line 38\r\n   
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()\r\n   
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)"
4

1 に答える 1

1

主な問題は、 にカスケードがWorkOrder.WorkOrderStatusないため、NHibernate は、保存時にそのコレクションで見つかった変更を保持することを認識できないことです。

WorkOrder.hbm.xml私は次のように変更しましたset

<set name="WorkOrderStatus" table="tblWorkOrderStatus" inverse="true"  cascade="all-delete-orphan">
  <key column="StatusID" />
  <one-to-many class="TelDir.Core.Domain.WorkOrderStatus, TelDir.Core" />
</set>

そして、このテストに合格しました:

    // Arrange
    var workorder = new WorkOrder("001", "Test-001");
    var status = new Status("1", "Status-1");

    workorder.AddStatus(status, DateTime.Now);

    WorkOrderStatus expected;

    // Act
    using (ISession session = _factory.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        session.Save(status);
        session.SaveOrUpdate(workorder);

        tx.Commit();
    }

    using (ISession session = _factory.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        expected = session.Query<WorkOrderStatus>()
            .Fetch(s => s.Status)
            .Fetch(s => s.WorkOrder)
            .FirstOrDefault();
    }

    // Assert
    expected.Should().NotBeNull();
    expected.Status.Should().Be(status);
    expected.WorkOrder.Should().Be(workorder);

削除する

ISet<WorkOrderStatus> WorkOrderStatusのプロパティのコメントを外しますStatus。また、<set name="WorkOrderStatus" ...Status.hbm.xml のコメントを外し、 でcascade="all-delete-orphan"行ったように属性を追加しますWorkOrder

作業指示書に追加:

public void RemoveStatus(WorkOrderStatus item)
{
    if (!WorkOrderStatus.Contains(item)) return;

    item.Status.WorkOrderStatus.Remove(item);
    WorkOrderStatus.Remove(item);
}

これで、このテストに合格するはずです。

    // Arrange
    var workorder = new WorkOrder("001", "Test-001");
    var status = new Status("1", "Status-1");

    workorder.AddStatus(status, DateTime.Now);

    WorkOrderStatus expected;

    // Act
    using (ISession session = _factory.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        session.Save(status);
        session.SaveOrUpdate(workorder);

        tx.Commit();
    }

    using (ISession session = _factory.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        expected = session.Query<WorkOrderStatus>()
            .Fetch(s => s.Status)
            .Fetch(s => s.WorkOrder)
            .FirstOrDefault();

        expected.WorkOrder.RemoveStatus(expected);

        tx.Commit();
    }

    using (ISession session = _factory.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        expected = session.Query<WorkOrderStatus>()
            .Fetch(s => s.Status)
            .Fetch(s => s.WorkOrder)
            .FirstOrDefault();
    }

    // Assert
    expected.Should().BeNull();
于 2012-06-20T04:10:50.293 に答える