开发者

hibernate - while updating NonUniqueObjectException with pessimistic lock "upgrade"

hibernate 3.6.0 final

i have used pessimistic-lock

LockOptions - because LockMode is not working i.e "select ... for update" "for update" is not generating in sql that's why.

at session.saveOrUpdate ERROR :

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}

RateCode.hbm.xml

<class catalog="hermes" name="com.hermes.data.RateCode" table="ratecodes">
    <composite-id class="com.hermes.data.RateCodeId" name="id">
      <key-property name="roomId" type="int">
        <column name="roomId"/>
      </key-property>
      <key-property name="date" type="date">
        <column length="10" name="date" />
      </key-property>
    </composite-id>

java Code

for (int i = 0; i < 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() > 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();
                }

............... getting list with the same session and/or another session.

with merge

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}

public List<RateCode> 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>=:from and rr.id.date<=: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();
    }


saveOrUpdate takes a detached instance (newRateCodeRange) and tries to attach it to the session. Its documentation says that an exception is thrown (the one you're encountering) if another instance of the same entity with the same ID is already associated with the session, which is the case here (existingRateCode).

You should probably use the merge method in order to copy the state of the detached instance to the attached one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜