Page 1 of 1

a different object with the same identifier value was...

Posted: Wed May 01, 2013 3:15 pm
by AbleMods
I'm trying to add an order-note to an existing order. I can't even get it to work a simple page. I still get the error. It seems like such a basic thing to do. Do I need to restart the whole app or something? What am I doing wrong?

Here's the only code:

Code: Select all

    protected void Page_Load(object sender, EventArgs e)
    {
        OrderNote _Note = new OrderNote();
        _Note.OrderId = 1;
        _Note.NoteType = NoteType.SystemPrivate;
        _Note.Comment = "test order note by Joe";
        _Note.Save();
    }
And here's the error I get every time:
a different object with the same identifier value was already associated with the session: 17, of entity: CommerceBuilder.Orders.OrderNote

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: NHibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 17, of entity: CommerceBuilder.Orders.OrderNote

Source Error:


Line 15: _Note.NoteType = NoteType.SystemPrivate;
Line 16: _Note.Comment = "test order note by Joe";
Line 17: _Note.Save();
Line 18: }
Line 19: }

Source File: c:\AbleMods-Modules\ReturnsManager-Gold\Site\test-ordernote.aspx.cs Line: 17

Stack Trace:


[NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 17, of entity: CommerceBuilder.Orders.OrderNote]
CommerceBuilder.DomainModel.EntityWithTypedId`1.Save() +636
test_ordernote.Page_Load(Object sender, EventArgs e) in c:\AbleMods-Modules\ReturnsManager-Gold\Site\test-ordernote.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

Re: a different object with the same identifier value was...

Posted: Thu May 02, 2013 4:04 am
by jmestep
I was getting a similar error in a routine I was using to import order items from a 7.0.2 store with custom code, but using Able's methods. John told me he had run into the same problem and found a fix, but he hasn't put his code into our SVN yet so I can look at it. It was something like flushing the session after each save.

Re: a different object with the same identifier value was...

Posted: Thu May 02, 2013 6:41 am
by AbleMods
I can't even get it to work on the first save. The above is the only code in the page.

What's weird is that it is saving the record to the table. But it's like nHibernate is firing the .Save() twice or something.

I just don't get this nHibernate.

Re: a different object with the same identifier value was...

Posted: Thu May 02, 2013 7:22 am
by AbleMods
Ok figured it out.

You have to start a new transaction and commit the transaction. No idea why, seems counter-intuitive to me. But it works.

Code: Select all

    protected void Page_Load(object sender, EventArgs e)
    {
        AbleContext.Current.Database.BeginTransaction();
        OrderNote _Note = new OrderNote();
        _Note.OrderId = 4;
        _Note.NoteType = NoteType.SystemPrivate;
        _Note.Comment = "test order note by Joe";
        _Note.UserId = AbleContext.Current.UserId;
        _Note.Save();
        AbleContext.Current.Database.CommitTransaction();
    }