Results 1 to 3 of 3

Thread: a different object with the same identifier value was already associated with the ses

  1. #1

    Default Hibernate Session have two diffrent Object same identifier value

    Hi,

    I am using following code to update SolutionCategory,
    simple update without validation is working fine.....


    public void update(SolutionCategory o, Errors e) throws ServiceValidationException {

    SolutionCategory solutionCategory = (SolutionCategory) o;
    DynamicCriteria dynaCrit = new DynamicCriteria();
    dynaCrit.add("name",solutionCategory.getName(),Dyn amicCriteria.EQUAL);
    List list = findAll(dynaCrit);

    SolutionCategory tempSC = (SolutionCategory) list.get(0);
    if(list.size() > 0 && !tempSC.getID().equals(o.getID()) ){
    e.reject("DUPLICATE_CATEGORY_NAME");
    throw new ServiceValidationException(e);
    }

    dao.update(solutionCategory);
    }


    Before updating the record I want to validate duplicate SolutionCategory Name.
    So to implement that stuff I am going to fetch all record by :- List list = findAll(dynaCrit); method.

    Now after this validation code while executing dao.update(solutionCategory); line my hibernate session has two different object with the same identifier value i). Original Object of the Update function
    ii). another one is found by findAll method

    Because of this two object, at time of saving record following error has been generate by hibernate.

    a different object with the same identifier value was already associated with the session: [com..cs.solutionreg.solutioncategory.SolutionCateg ory#12]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.cs.solutionreg.solutioncategory.SolutionCatego ry#12]

    Could you give me proper solution?

    i). How I detach extra Object form Hibernate Session ??
    ii). Should not use this kind of validation in update function ??
    Last edited by dhavalpatel03; Apr 2nd, 2007 at 10:50 AM.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    The problem here is that you pass in a detached object which gets reattached to the Session. You also fetch the same object with the find. This causes the exception. You could change the query to do all the validation you require and therefore don't return it into the Session. Otherwise you'd have to evict the entity returned from the query.

  3. #3
    Join Date
    Jan 2007
    Location
    Kuala Lumpur, Malaysia
    Posts
    138

    Default Similar problem, with @GeneratedValue

    here is the problem,
    i'm using mysql 5 innodb as the storage engine
    and i use a Long id as the PK

    this is what i did
    insert - ID 1 (generated)
    insert - ID 2 (generated)
    insert - ID 3 (generated)
    delete all
    insert - ID 1 (generated) //exception

    Code:
    org.springframework.orm.hibernate3.HibernateSystemException
    a different object with the same identifier value was already associated with the session:

Posting Permissions

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