JPA自定义JDBC批处理大小不起作用

时间:2017-07-06 14:11:36

标签: java hibernate jpa orm

我尝试配置@Vlad Mihalcea博客中提到的JDBC批量大小 https://vladmihalcea.com/how-to-customize-the-jdbc-batch-size-for-each-persistence-context-with-hibernate/

EntityManager entityManager =  entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.unwrap(Session.class).setJdbcBatchSize(5);
        for(int i = 0;i<10;i++){
            Charge c = new Charge();
            c.setAccountNumber("acct"+i);
            entityManager.persist(c);
        }
entityManager.getTransaction().commit();



<bean id="entityManagerFactoryDefault" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="charg" />
        <property name="persistenceUnitName" value="MaterializedView" />
        <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false" />
                <property name="showSql" value="true"/> 
                <property name="database">
                    <util:constant static-field="org.springframework.orm.jpa.vendor.Database.ORACLE" />
                </property>
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.default_batch_fetch_size">500</prop>
                <prop key="hibernate.jdbc.fetch_size">10</prop>
                <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
                <prop key="hibernate.order_updates">true</prop>
                <prop key="hibernate.order_inserts">true</prop>
                <prop key="hibernate.jdbc.batch_size">30</prop>
                <prop key="hibernate.id.new_generator_mappings">false</prop>
            </props>
        </property>

    </bean>

但是它会触发10个插入查询。

我得到了这个问题 Hibernate:从dual中选择charge.nextval Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?) Hibernate:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)

我使用hibernate 5.2.10.Final版本和序列作为策略。 如果我的代码中有错误,有人可以纠正我吗

1 个答案:

答案 0 :(得分:2)

Hibernate日志记录机制可能会误导你。

使用datasource-proxy查看批处理是否有效。查看this article for more details