title: hibernate - while updating NonUniqueObjectException with pessimistic lock "upgrade"
body: <p>hibernate 3.6.0 final</p>
  
  <p>i have used pessimistic-lock</p>
  
  <p>LockOptions  - because LockMode is not working i.e "select ... for update" "for update" is not generating in sql that's why.</p>
  
  <p>at <code>session.saveOrUpdate</code> 
  ERROR : </p>
  
  <blockquote>
    <p>org.hibernate.NonUniqueObjectException:
    a different object with the same
    identifier value was already
    associated with the session:
    [com.hermes.data.RateCode#RateCodeId{roomId=6836date=2011-02-01}</p>
  </blockquote>
  
  <p>RateCode.hbm.xml</p>
  
  <pre><code>&lt;class catalog="hermes" name="com.hermes.data.RateCode" table="ratecodes"&gt;
      &lt;composite-id class="com.hermes.data.RateCodeId" name="id"&gt;
        &lt;key-property name="roomId" type="int"&gt;
          &lt;column name="roomId"/&gt;
        &lt;/key-property&gt;
        &lt;key-property name="date" type="date"&gt;
          &lt;column length="10" name="date" /&gt;
        &lt;/key-property&gt;
      &lt;/composite-id&gt;
  </code></pre>
  
  <p>java Code</p>
  
  <pre><code>for (int i = 0; i &lt; roomId.length; i++) {
                  existingRateCodeList = getRateCodeRoom(session, Integer.parseInt(roomId[i]), newRateCodeRange.getStartDate(), newRateCodeRange.getEndDate(), true, LockOptions.UPGRADE);
                  updateRecord = existingRateCodeList.size();
                  remainingRecord = totleRecord - updateRecord;
  
                  if (existingRateCodeList.size() &gt; 0) {
                      //update
                      updateOccured = true;
                      it = existingRateCodeList.iterator();
                      while (it.hasNext()) {
                          existingRateCode = (RateCode) it.next();
  
                          updatedRecordList.add(existingRateCode.getId());
                          newRateCodeRange.setId(existingRateCode.getId());
  
                          session.saveOrUpdate(newRateCodeRange);
                          session.flush();
                          session.clear();
                      }
                      tx.commit();
                  }
  </code></pre>
  
  <p>...............
  getting list with the same session and/or another session.</p>
  
  <p>with merge</p>
  
  <blockquote>
    <p>ERROR:
    org.hibernate.StaleObjectStateException:
    Row was updated or deleted by another
    transaction (or unsaved-value mapping
    was incorrect):
    [com.hermes.data.RateCode#RateCodeId{roomId=6836date=2011-02-01}</p>
  </blockquote>
  
  <pre><code>public List&lt;RateCode&gt; getRateCodeRoom(Session session, int roomId, Date from, Date to, boolean refreshCache, LockOptions lockOptions) {
  
          Query q = session.createQuery(
                  "from RateCode rr where rr.id.roomId=:roomId and rr.id.date&gt;=:from and rr.id.date&lt;=:to order by rr.id.date").setInteger("roomId", roomId).setParameter("from", from).setParameter("to", to).setCacheable(true).setCacheMode(refreshCache ? CacheMode.REFRESH : CacheMode.NORMAL);
  
          if (lockOptions != null) {
              q.setLockOptions(lockOptions);
          }
          return q.list();
      }
  </code></pre>