hibernate批量插入

时间:2014-12-29 17:19:19

标签: java hibernate spring-batch

在java中使用hibernate插入批处理假设要像插入块(name,id)值一样创建(' name0',1),(' name1',2 ),(' name2',3),(' name3',4)?为什么我的应用程序没有为我的批处理创建连接插入?

这是我的hibernate配置:

<props>
    <prop key="hibernate.jdbc.batch_size">${iscsi.batch.size.save.blocks}</prop>
    <prop key="hibernate.jdbc.fetch_size">25</prop>
    <prop key="hibernate.order_inserts">true</prop>
    <prop key="hibernate.order_updates">true</prop>
    <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
    <prop key="hibernate.hbm2ddl.auto">none</prop>

    <prop key="hibernate.c3p0.min_size">2</prop>
    <prop key="hibernate.c3p0.max_size">100</prop>
    <prop key="hibernate.c3p0.timeout">100</prop>
    <prop key="hibernate.c3p0.max_statements">0</prop>
    <prop key="hibernate.c3p0.maxIdle">-1</prop>
    <prop key="hibernate.c3p0.idle_test_period">100</prop>
    <prop key="hibernate.c3p0.acquire_increment">1</prop>
    <prop key="hibernate.c3p0.unreturnedConnectionTimeout">30</prop>
    <prop key="hibernate.c3p0.debugUnreturnedConnectionStackTraces">false</prop>
</props>

这是我的批次代码:

Session session = null;
Transaction transaction = null;
// Listas usadas para retornar todos os objetos alterados
List<BlockIscsi> listReturn = new ArrayList<BlockIscsi>();
List<BlockIscsi> listSwap = new ArrayList<BlockIscsi>();

Iterator<BlockIscsi> it = list.iterator();
while (it.hasNext()) {
    try {
        int i = 0;
        session = currentSession();
        transaction = session.beginTransaction();

        while (i < batchSize && it.hasNext()) {
            BlockIscsi blockIscsi = it.next();
            session.save(blockIscsi);
            listSwap.add(blockIscsi);
            i++;
        }
        session.flush();
        listReturn.addAll(listSwap);
        listSwap = new ArrayList<BlockIscsi>();
    } catch (ConstraintViolationException e) {
        LOGGER.error("Error on save blockIscsi: ", e);
        System.out.println("Error on save blockIscsi : " + e.getMessage());
        // e.printStackTrace();
    } catch (NonUniqueObjectException e) {
        LOGGER.error("Error on save blockIscsi: ", e);
        System.out.println("Error on save blockIscsi : " + e.getMessage());
        // e.printStackTrace();
    } catch (JDBCConnectionException e) {
        LOGGER.error("Error on save blockIscsi list : ", e);
        // e.printStackTrace();
    } catch (Exception e) {
        LOGGER.error("Error on save blockIscsi: ", e);
        System.out.println("Error on save blockIscsi: " + e.getMessage());
        e.printStackTrace();
        // throw new RuntimeException(e);
    } finally {
        transaction.commit();
    }
}

0 个答案:

没有答案