Hibernate:陈旧状态异常

时间:2015-06-15 11:57:21

标签: java spring hibernate spring-mvc transactions

我正在开发一个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)

我做错了什么。你能帮忙的话,我会很高兴。非常感谢。 : - )

1 个答案:

答案 0 :(得分:1)

异常的原因是您之前加载了GroupCanvas,并且它具有对GroupSection的引用。然后删除GroupSection,但是当事务提交时,GroupCanvas仍然保留对已删除的GroupSection的引用,并且您获得StaleStateException。

如您所见,首先删除GroupSection会阻止GroupCanvas加载已删除的GroupSection。

作为替代方案,您也可以这样做:

  create or replace type SUBTYP2 as OBJECT
  (
     AGE NUMBER,
     typ1 TABLETYP1
  );
  /

   create type TABLETYP2 AS TABLE OF SUBTYP2;
  /


DECLARE
 ty2 TABLETYP2 := TABLETYP2() ;
 ty1 TABLETYP1 := TABLETYP1() ;

 v_age number;
 v_name varchar2(10);
BEGIn
 ty1.extend;
 ty1(1) :=  SUBTYP1(12,'Mahesh');

 ty2.extend;
 ty2(1) :=  SUBTYP2(14,ty1);

 SELECT AGE into v_age from TABLE(ty2);

 dbms_output.put_line(v_age);
END;
/