java - 组织.hibernate.StaleStateException : Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

标签 java mysql hibernate stalestateexception

有时候在持久化一个obj的时候,它的某个字段太大,无法放入db字段,导致数据截断异常。在下面的代码中,我 try catch DataException 并简单地清空该字段,然后重新保存。但是,我在重新保存时遇到异常。为什么会出现批量更新异常,我该如何解决?


 public static void save(Object obj) throws Exception{
        try{
            beginTransaction();
            getSession().save(obj);
            commitTransaction();

        }catch(Exception e){
            e.printStackTrace();
            rollbackTransaction();
            throw e;
        }finally{
            closeSession(); //not needed, session obtained from sf.getCurrentSession() will auto close
        }
    }   
 public static void saveXXX(XXX rec){

        try {
            save(rec);
        } catch (org.hibernate.exception.DataException e) {
            e.printStackTrace();

            saveXXX(rec, e); //causes an exception      
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private static void saveXXX(WhoisRecord rec, DataException e) {
        rec.setField(""); //empty out the problem field
        saveXXX(rec);

异常:

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)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2382)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2335)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2635)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
.
.
.

最佳答案

我不是这方面的专家,但我想我只是遇到了同样的问题,但方向相反。

看起来发生的事情是您尝试保存记录,而 Hibernate 抛出数据截断异常,这意味着它保存了一些但不是您想要的所有内容。所以你捕获了那个异常,并尝试回滚事务。但这并不总是保证成功(从我在 Hibernate save() and transaction rollback 上看到的),所以保存的数据可能仍然存在。让我们假设它是。接下来您更改您的记录,并尝试再次保存它。

但由于数据仍然存在,调用保存不会做任何事情,因为在这种情况下它应该是一个更新。因此,当您尝试提交该事务时,Hibernate 知道您要保存 1 条记录,但它成功保存了 0 条记录,因此出现错误。

尝试将 getSession().save(obj); 更改为 getSession().saveOrUpdate(obj);。这应该在记录不存在时保存记录,并在记录存在时更新它(不存在过大的字段)。

关于java - 组织.hibernate.StaleStateException : Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086802/

相关文章:

mysql - 从数据库的所有mysql表名中删除前缀

php - 根据第一个选择值(车辆模型)从数据库中填充第二个选择 - codeigniter

java - hibernate 插入错误: ids for this class must be manually assigned before calling save():

java - 关于实体对象中空值的最佳实践

java - org.hibernate.MappingException : Could not determine type for: at table: for columns: [org. hibernate.mapping.Column(植物)

java - 无法从 START_ARRAY token 中反序列化 java.lang.String 的实例;

java - 构建有向循环图的流畅界面?

java - 一种每天分析和计算大量 Oracle 数据的方法

java - R.layout.main的原始类型int没有字段out

android - Mysql数据库回调