本文介绍了hibernate插入带分区postgresql的批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有解决方案批量插入通过hibernate在分区postgresql表?目前我收到一个这样的错误...

  ERROR org.hibernate.jdbc.AbstractBatcher  - 执行批次的异常:
org.hibernate.StaleStateException:批量更新从update [0]返回意外的行数;实际行数:0;预期:1
在org.hibernate.jdbc.Expectations $ BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations $ BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)....

我发现这个链接,但我无法在网上找到任何地方是这个问题解决或如何可以解决

解决方案

您可能想通过设置hibernate.jdbc.factory_class属性来尝试使用自定义的Batcher。确保hibernate不会检查批处理操作的更新计数可能会解决您的问题,您可以通过使您的自定义Batcher扩展BatchingBatcher类,然后覆盖方法doExecuteBatch(...)来看起来像:

  @Override 
保护void doExecuteBatch(PreparedStatement ps)抛出SQLException,HibernateException {
if(batchSize == 0) {
log.debug(no batched statements to execute);

else {
if(log.isDebugEnabled()){
log.debug(Executing batch size:+ batchSize);
}

try {
// checkRowCounts(ps.executeBatch(),ps);
ps.executeBatch();

catch(RuntimeException re){
log.error(Exception execution batch:,re);
throw re;
}
finally {
batchSize = 0;
}

}

}




is there a solution for batch insert via hibernate in partitioned postgresql table? currently i'm getting an error like this...

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:61)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
   at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)....

i have found this link http://lists.jboss.org/pipermail/hibernate-dev/2007-October/002771.html but i can't find anywhere on the web is this problem solved or how it can be get around

解决方案

You might want to try using a custom Batcher by setting the hibernate.jdbc.factory_class property. Making sure hibernate won't check the update count of batch operations might fix your problem, you can achieve that by making your custom Batcher extend the class BatchingBatcher, and then overriding the method doExecuteBatch(...) to look like:

    @Override
    protected void doExecuteBatch(PreparedStatement ps) throws SQLException, HibernateException {
        if ( batchSize == 0 ) {
            log.debug( "no batched statements to execute" );
        }
        else {
            if ( log.isDebugEnabled() ) {
                log.debug( "Executing batch size: " + batchSize );
            }

            try {
//              checkRowCounts( ps.executeBatch(), ps );
                ps.executeBatch();
            }
            catch (RuntimeException re) {
                log.error( "Exception executing batch: ", re );
                throw re;
            }
            finally {
                batchSize = 0;
            }

        }

    }

Note that the new method doesn't check the results of executing the prepared statements. Keep in mind that making this change might affect hibernate in some unexpected way (or maybe not).

这篇关于hibernate插入带分区postgresql的批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:38