首页 > 解决方案 > BatchedTooManyRowsAffectedException 的解决方案是什么?

问题描述

目前,我们在从特定表中删除记录时遇到以下异常。我浏览了许多博客和论坛,我才知道它的发生是因为我们从 DB 中删除了 2 条重复记录,但 hibernate 期望删除 1 条记录。如果我错了,请纠正我。

我们认为这个问题将在 Oracle 12c 第 2 版中得到解决。但即使在升级 Oracle 之后,我们仍然面临这个问题。

如果您可以为此问题提供解决方案,那将非常有帮助。

例外 :

org.springframework.orm.hibernate3.HibernateSystemException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1; nested exception is org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:103)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:155)
    at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:192)
    at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:150)

Oracle 数据库版本: Oracle 12c 第 2 版

驱动程序版本: 11.2

OJDBC 罐: ojdbc6-11.2.0.3

休眠版本: 4.1.7

参考: Oracle JDBC batchUpdate 受影响的行始终为 -2 (Statement.SUCCESS_NO_INFO)

休眠 - 批量更新从更新返回意外的行数:0 实际行数:0 预期:1

标签: javaoraclehibernatejdbc

解决方案


我访问了这个链接,https://hibernate.atlassian.net/browse/HHH-9134,有人在保存时解决了同样的问题。

    Article article = new Article();
    article.setName("test");
    List<Section> articles = article.getSections(); //ArrayList
    Section section1 = new Section();
    section1.setName("test");
    section1.setContent("test");
    articles.add(section1);
    Section section2 = new Section();
    section2.setName("test");
    section2.setContent("test");
    articles.add(section2);
    Section section3 = new Section();
    section3.setName("test");
    section3.setContent("test");
    articles.add(section3);
    session.save(article);  //throw exception here

    Article article = articleManager.get(1L);
    article.setName("test");
    List<Section> articles = new ArrayList<Section>();
    Section section1 = new Section();
    section1.setName("test");
    section1.setContent("test");
    articles.add(section1);
    Section section2 = new Section();
    section2.setName("test");
    section2.setContent("test");
    articles.add(section2);
    Section section3 = new Section();
    section3.setName("test");
    section3.setContent("test");
    articles.add(section3);
    article.setSections(sections);
    session.save(article);  //throw exception here

    Article article = articleManager.get(1L);
    article.setName("test");
    List<Section> articles = article.getSections(); //PersistentList
    articles.clear();
    Section section1 = new Section();
    section1.setName("test");
    section1.setContent("test");
    articles.add(section1);
    Section section2 = new Section();
    section2.setName("test");
    section2.setContent("test");
    articles.add(section2);
    Section section3 = new Section();
    section3.setName("test");
    section3.setContent("test");
    articles.add(section3);
    session.save(article);  // it works fine 

推荐阅读