【发布时间】:2014-11-29 15:58:29
【问题描述】:
我有一个 Java 类 ClassA,这个类定义了休眠映射。 在代码的某处,我需要检索保存在数据库中的此类的所有实例:
public List<ClassA> getAllClassA(){
Criteria crit = getSession().createCriteria(ClassA.class);
return crit.list();
}
allA = getAllClassA();
稍后我将删除一些对象,同时更新其他对象:
public void removeItem() {
Iterator<ClassA> it = allA.iterator();
while(it.hasNext()){
ClassA cA = it.next();
if(cA.hasCondition()){
dao.remove(cA);
it.remove();
}
else {
cA.update();
}
}
dao.update(allA);
}
//this is in another class
public void update(List<ClassA> allA){ //dao.update
for(ClassA cA: allA){
getSession().saveOrUpdate(cA);
}
}
发生的情况是数据库已正确更新(并删除了所需的对象),但它也吐出以下错误:
错误 org.hibernate.event.def.AbstractFlushingEventListener.performExecutions:324 - 无法将数据库状态与会话 org.hibernate.StaleStateException 同步:批量更新返回意外 来自更新 [0] 的行数;实际行数:0;预计:1
我知道 Stack Overflow 上还有其他类似的问题,但它们似乎是由不同的条件引起的,在这种情况下没有用。
有什么想法吗?
【问题讨论】:
标签: java database hibernate jpa orm