【发布时间】:2011-02-22 13:04:20
【问题描述】:
休眠 3.6.0 最终版
我用过悲观锁
LockOptions - 因为 LockMode 不起作用,即“select ... for update”“for update”没有在 sql 中生成,这就是原因。
session.saveOrUpdate
错误:
org.hibernate.NonUniqueObjectException: 不同的对象具有相同的 标识符值已经 与会话相关联: [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代码
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();
}
....... 获取相同会话和/或另一个会话的列表。
合并
错误: org.hibernate.StaleObjectStateException: 行已被其他人更新或删除 交易(或未保存的值映射 不正确): [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();
}
【问题讨论】:
标签: hibernate hibernate-mapping