Hibernate:陈旧状态异常

Hibernate : Stale state exception

我正在开发一个 Spring-MVC 应用程序,我试图在其中从数据库中删除一个对象。几天前,这个错误突然出现,现在我无法删除。我在网上查了一下,但找不到我做错了什么,none 的解决方案似乎有效。

服务层代码:

  @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;
    }