tags:

views:

387

answers:

1

Hello, I am getting the following error when trying to update an object in a db. Does anyone have any idea what might be happening? I have checked all my datatypes and they correspond to what is in the db. Thanks for any thoughts -

NHibernate.HibernateException was unhandled by user code
  Message="identifier of an instance of DataTransfer.status was altered from 3 to 4"
  Source="NHibernate"
  StackTrace:
       at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode)
       at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session)
       at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
       at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event)
       at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
       at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
       at NHibernate.Impl.SessionImpl.Flush()
       at NHibernate.Transaction.AdoTransaction.Commit()
       at DataAccessLayer.NHibernateDataProvider.UpdateItem_temp(items_temp item_temp) in C:\Documents and Settings\Carl.PAMB\My Documents\Visual Studio 2008\Projects\InventoryDataClean\DataAccessLayer\NHibernateDataProvider.cs:line 226
       at InventoryDataClean.Controllers.ImportController.Edit(Int32 id, FormCollection formValues) in C:\Documents and Settings\Carl.PAMB\My Documents\Visual Studio 2008\Projects\InventoryDataClean\InventoryDataClean\Controllers\ImportController.cs:line 101
       at lambda_method(ExecutionScope , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

From my log4net log -

13:37:17 [9] DEBUG NHibernate.Event.Default.DefaultSaveOrUpdateEventListener - object already associated with session: [DataTransfer.items_temp#56876]

Also, here is where it is called -

 item.status.id = Int32.Parse(formValues["Status"]);
    _provider.UpdateItem_temp(item);

And here is the code from my data provider.

public void UpdateItem_temp(items_temp item_temp)
        {
            ITransaction t = _session.BeginTransaction();
            try
            {
                _session.SaveOrUpdate(item_temp);
                t.Commit();
            }
            catch (Exception)
            {
                t.Rollback();
               throw;
            }
            finally
            {
                t.Dispose();
            }
        }
+1  A: 

This is definitely wrong:

item.status.id = Int32.Parse(formValues["Status"]);

You are changing the Id of a status instance, which is illegal.

Instead, if you are trying to change the item status, you should be doing this:

item.status = session.Load<Status>(Int32.Parse(formValues["Status"]));

(I guessed "Status" as the type of the status property; replace it with the correct name)

Diego Mijelshon
+1 for the first part. We don't really know about the second part because no data object definitions were posted, but that's largely irrelevant to the problem.
Jon Seigel
That's the ticket - worked perfectly. Thanks
czuroski