3

hibernatetemplatebulkUpdate真的在做bulkUpdate吗?我看了看代码,好像没有bulkUpdate。或者,也许我错过了什么?

public int bulkUpdate(final String queryString, final Object... values) throws DataAccessException {
        return executeWithNativeSession(new HibernateCallback<Integer>() {
            public Integer doInHibernate(Session session) throws HibernateException {
                Query queryObject = session.createQuery(queryString);
                prepareQuery(queryObject);
                if (values != null) {
                    for (int i = 0; i < values.length; i++) {
                        queryObject.setParameter(i, values[i]);
                    }
                }
                return queryObject.executeUpdate();
            }
        });
    }

JdbcTemplate batchUpdate(看起来)正在做batchUpdate

public int[] batchUpdate(final String[] sql) throws DataAccessException {
        Assert.notEmpty(sql, "SQL array must not be empty");
        if (logger.isDebugEnabled()) {
            logger.debug("Executing SQL batch update of " + sql.length + " statements");
        }
        class BatchUpdateStatementCallback implements StatementCallback<int[]>, SqlProvider {
            private String currSql;
            public int[] doInStatement(Statement stmt) throws SQLException, DataAccessException {
                int[] rowsAffected = new int[sql.length];
                if (JdbcUtils.supportsBatchUpdates(stmt.getConnection())) {
                    for (String sqlStmt : sql) {
                        this.currSql = sqlStmt;
                        stmt.addBatch(sqlStmt);
                    }
                    rowsAffected = stmt.executeBatch();
                }
                else {
                    for (int i = 0; i < sql.length; i++) {
                        this.currSql = sql[i];
                        if (!stmt.execute(sql[i])) {
                            rowsAffected[i] = stmt.getUpdateCount();
                        }
                        else {
                            throw new InvalidDataAccessApiUsageException("Invalid batch SQL statement: " + sql[i]);
                        }
                    }
                }
                return rowsAffected;
            }
            public String getSql() {
                return this.currSql;
            }
        }
        return execute(new BatchUpdateStatementCallback());
    }
4

1 回答 1

0

是的,它正在进行批量更新。如您所见,在方法中执行的查询DELETE可能影响多行。这就是为什么它们被称为批量操作。INSERTbulkUpdate

要点是有方便的方法来执行更新和执行查询并返回批量操作中受影响的行数。此外,它还包装了 DataAccessException 的异常。

于 2012-06-28T05:10:19.143 回答