Results 1 to 3 of 3

Thread: A different object with same identifier was already associated with the session

  1. #1
    Join Date
    Aug 2007
    Posts
    6

    Unhappy A different object with same identifier was already associated with the session

    Hi,

    I am getting problems while updating objects into database. I am using NHibernate 1.2 GA and Spring 1.1 RC.

    I have 3 domain objects with following relations.

    Item, XmpieDocument , XmpieFields

    Item --> XmpieDocument (Many-to-one)
    XmpieDocument --> XmpieFields (one-to-Many)

    In my code, I access Item object then its XmpieDocument object then I access a collection of XmpieFields. If XmpieFields is null then I assign new values and save through following code which works fine in case of Save but if XmpieFields is not null but I assign it a new collection value and try to update it then it throws an error. My code is below.

    Item item = ItemDao.FindByID(item.ID);
    item.XmpieDocument.XmpieFields = GetXmpieFields();
    ItemDao.SaveOrUpdateCopy(item);

    it works fine on Save but in case of update I get following error.

    {"a different object with the same identifier value was already associated with the session: 1, of class: MyCompany.DataAccessLayer.BulkOrder.XmpieFields"}

    Any ideas/help for how to resolve it, is highly appreciated as I am really stuck here and dont know what to do.

    Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    Bergen, Norway
    Posts
    365

    Default

    Hi!

    Here is your code:
    Code:
     Item item = ItemDao.FindByID(item.ID);
      item.XmpieDocument.XmpieFields = GetXmpieFields();
      ItemDao.SaveOrUpdateCopy(item);
    It seems that you already have the item or parts of the item loaded and you are using this object reference to reload the entity:
    Item item = ItemDao.FindByID(item.ID);

    You have extracted all the XmpieFields from the previous item state and possibly modified at least on or added more fields to the collection which you set back on the item's XmpieDocument:
    item.XmpieDocument.XmpieFields = GetXmpieFields();

    Thus, when you call SaveOrUpdate, then there might be persistent objects that is already attached to the current running session in the object graph and these must be evicted from the session to avoid this exception [by calling session.Evict(...)].

    Based on the exception, I would say it's an instance of this class that must be evicted from the session: MyCompany.DataAccessLayer.BulkOrder.XmpieFields.

    HTH,
    Steinar.

  3. #3
    Join Date
    Jun 2010
    Posts
    1

    Lightbulb

    This happens because you are trying to use saveOrUpdate(foo) with foo being a newly created object, and another object with the same id in the hibernate cache, it means that you have loaded into cache a hibernate object and you are trying to save a different one with the same id.

    one way to avoit it is to use a dao method like this one:

    public Car saveOrUpdate(Car foo) {
    if (foo.getId() != null) { // it is an update
    return session.merge(foo);
    } else { // you are saving a new one
    return session.saveOrUpdate(foo)
    }


    }


    before realizing that was the right way I was using clear, and it works, but with serious implications, I would recommend you to read the clear() method docs before ever trying to use it.


    public Car saveOrUpdate(Car foo) {

    session.clear();
    return session.saveOrUpdate(foo)



    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •