我正在尝试在Spring-MVC应用程序中尝试从数据库中删除对象。几天前,这个错误突然开始了,现在我无法删除。我在网上检查了一下,但是找不到我在做什么,而且所有解决方案似乎都无效。
服务层代码:
@Override
public boolean deleteGroupSection(int sectionId, int mcanvasId) {
try {
GroupCanvas groupCanvas = this.groupCanvasService.getCanvasById(mcanvasId);
Long groupAccountId = this.groupAccountService.getGroupById(groupCanvas.getGroupAccountId()).getGroupId();
this.groupAttachmentsService.deleteAttachmentsForSection(sectionId,groupAccountId);
}catch (Exception ignored){}
this.groupSectionDAO.deleteGroupSection(sectionId,mcanvasId);
return true;
}
DAO代码:
@Override
public boolean deleteGroupSection(int sectionid, int mcanvasId) {
Session session = this.sessionFactory.getCurrentSession();
GroupSection groupSection = (GroupSection) session.get(GroupSection.class,sectionid);
session.delete(groupSection);
session.flush();
return true;
}
错误代码 :
SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] with root cause
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at com.journaldev.spring.dao.GroupSectionDAOImpl.deleteGroupSection(GroupSectionDAOImpl.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
我究竟做错了什么。你能帮忙的话,我会很高兴。非常感谢。 :-)
最佳答案
发生异常的原因是您之前加载过GroupCanvas,并且它具有对GroupSection的引用。然后,您删除GroupSection,但是在事务提交时,GroupCanvas仍然保留对已删除的GroupSection的引用,并且您会收到StaleStateException。
如您所见,首先删除GroupSection可以防止GroupCanvas加载已经删除的GroupSection。
另外,您也可以执行以下操作:
@Override
public boolean deleteGroupSection(int sectionId, int mcanvasId) {
try {
GroupCanvas groupCanvas = this.groupCanvasService.getCanvasById(mcanvasId);
Long groupAccountId = this.groupAccountService.getGroupById(groupCanvas.getGroupAccountId()).getGroupId();
this.groupAttachmentsService.deleteAttachmentsForSection(sectionId,groupAccountId);
}catch (Exception ignored){}
this.groupSectionDAO.deleteGroupSection(sectionId,mcanvasId);
for(Iterator<GroupSection> it = groupCanvas.getGroupSections().iterator(); it.hasNext();){
GroupSection gs = it.next();
if(gs.getId().equals(sectionId)){
it.remove(gs);
}
}
return true;
}
关于java - hibernate :过时状态异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30844601/