gpt4 book ai didi

java - hibernate : Stale state exception

转载 作者:行者123 更新时间:2023-12-01 11:25:29 24 4
gpt4 key购买 nike

我正在开发一个 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 : Stale state exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30844601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com