title: hibernate NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
body: <p>My Code:</p>
  
  <p>I am trying to do either update/insert. It gets inserted properly, only problem is when I try to update it gives below error message. </p>
  
  <pre><code>private String sessionType=null;
  public String getAccountsDetails(List&lt;Account&gt; accountList) {       
      Session session = sessionFactory.openSession();
      for (Account account : accountList) {
          AccountDetails accountDetails = new AccountDetails();
          accountDetails.setAccountsId(Long.parseLong(account.getId()));
          accountDetails.setAccounttype(account.getAccountType().value());
          Query query = session.createQuery("from QBAccounts qba where qba.accountsId=:accId");
          List&lt;AccountDetails&gt; queryList = query.setParameter("accId", accountDetails.getAccountsId()).list();
          if(queryList.size()&gt;0){         
              session.update(accountDetails);
          }else{
              session.save(accountDetails);
          }
      }
      session.flush();  
      session.beginTransaction().commit();
      session.clear();
      session.close();           
      return "Successfully data updated into table";
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.trinet.mulesoft.quickbooks.dto.AccountDetails#0]
      at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:638)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)        
  </code></pre>
  
  <hr>
  
  <p>EDIT 2:</p>
  
  <p>I have used</p>
  
  <blockquote>
    <p>session.merge(accountDetails)</p>
  </blockquote>
  
  <p>there is no error, but it always does insert data into db instead of update.</p>