我不知道如何处理以下事项:
saveOrUpdate
。合并后的实体将返回给客户端; saveOrUpdate
。该方法退出但实体未更新其version
号码并将其发送到客户端。现在,实体的版本号与数据库不同步。 数据库中的实体version
字段已正确更新; equals()
方法使用version
字段,因此它失败并且不会删除该实体。或者我在再次更新实体时获得OptimisticLockException
。这是Hibernate的正常行为吗?
服务类
public List<HtDayEntry> deleteHtDayEntry(String clientVersion,
@Lazy HtDayEntry htDayEntryToDelete, List<HtDayEntry> modifiedHtDayEntries)
throws ClientServerVersionMismatchException {
compareClientServerVersion(clientVersion);
htDayEntryToDelete = (HtDayEntry) htDayEntryDao.merge(htDayEntryToDelete);
// START: remove from the other side of the relationships
HtDay htDay = htDayEntryToDelete.getHtDay();
htDay.getHtDayEntries().remove(htDayEntryToDelete);
HtDayEntryType htDayEntryType = htDayEntryToDelete.getHtDayEntryType();
htDayEntryType.getHtDayEntries().remove(htDayEntryToDelete);
for (HtNote htNote : htDayEntryToDelete.getHtNotes()) {
htNote.setHtDayEntry(null);
}
// END: remove from the other side of the relationships
htDayEntryDao.delete(htDayEntryToDelete);
if (isHtDayEmpty(htDay)) {
htDayDao.delete(htDay);
}
if (modifiedHtDayEntries != null) {
HtDayEntry modifiedHtDayEntry;
ListIterator iterator = modifiedHtDayEntries.listIterator();
while(iterator.hasNext()){
modifiedHtDayEntry = (HtDayEntry) iterator.next();
modifiedHtDayEntry = (HtDayEntry) htDayEntryDao.merge(modifiedHtDayEntry);
htDayEntryDao.saveOrUpdate(modifiedHtDayEntry);
// START: must manually update the version field, I have no idea why
modifiedHtDayEntry.setVersion(modifiedHtDayEntry.getVersion() + 1);
iterator.set(modifiedHtDayEntry); // Update the modifiedHtDayEntry in the iterator so that the new version number can be returned
// END: must manually update the version field, I have no idea why
}
}
return modifiedHtDayEntries;
}
HtDayEntryDao
public void saveOrUpdate(Object object) throws HibernateException {
getSession().saveOrUpdate(object);
}
实体(超类中的方法)
private long version;
@Version
@Column(name = "version")
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}