kelezyl 2014-04-05 06:08 采纳率: 0%
浏览 1591

getHibernateTemplate().delete()删除记录是报错

SpingMVC+Hibernate删除某一条数据库不存在的记录的时候,就会报错误:
13:55:38.206 [http-8080-2] ERROR org.hibernate.jdbc.AbstractBatcher - Exception executing batch:
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:85) ~[hibernate3.jar:3.6.10.Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate3.jar:3.6.10.Final]
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate3.jar:3.6.10.Final]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate3.jar:3.6.10.Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) [hibernate3.jar:3.6.10.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) [hibernate3.jar:3.6.10.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189) [hibernate3.jar:3.6.10.Final]

自己使用try——catch也没有用

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-01 22:14
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题可能是由于Hibernate在尝试执行更新操作时遇到了问题。在Hibernate中,你可以通过设置@TransactionAttribute注解来控制事务的属性。

    以下是一个示例,展示了如何设置事务属性以处理这种情况:

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public void deleteRecord(int id) {
        getHibernateTemplate().delete(record);
    }
    

    在这个例子中,如果发生异常(例如,当删除失败时),那么rollbackFor参数指定的异常类型会被抛出,并且会触发自动提交或回滚。

    注意:这只是一个示例,实际应用中可能需要根据你的具体需求进行调整。

    评论

报告相同问题?