-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NHibernate Mapping many-to-many composite key & extra column
PostPosted: Wed Jun 20, 2012 11:38 pm 
Newbie

Joined: Thu Aug 11, 2011 6:06 am
Posts: 3
I am little bit new to NHibernate. I wish to implement a web application using asp.net using C#.

I have the following database schemas:

Image

Here are my mapping:
Code:
<?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" cascade="all-delete-orphan">
      <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> 



And my POCO class are defined as below:
Code:
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(); 
        }

    }



When I run this code from my Deault.aspx.cs

NHibernateDaoFactory daoFactory = new NHibernateDaoFactory();
IWorkOrderDao iobjWo = daoFactory.GetWorkOrderDao();
WorkOrder _objWo = iobjWo.GetById(1, false);
WorkOrderStatus _objWS = iobjWo.GetCurrentWorkOrderStatus(_objWo);
_objWo.RemoveStatus(_objWS);

I got following error:

The given key was not present in the dictionary.

And at Stacktrace contains the following message:

" 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)"



I do not really know what exactly where the cause of the problem. May be on my mapping files, of my POCO class, or somewhere else.

Could you please guide me to the right solution for this problem?

Best regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.