【问题标题】:Hibernate Batch Insert with Spring and Hibernate not working使用 Spring 和 Hibernate 的 Hibernate 批量插入不起作用
【发布时间】:2018-10-22 21:35:53
【问题描述】:

我无法让 Hibernate 在 MySQL 上执行批量插入。

我的主键生成类型为 Sequence,下面是我的 dao.xml

    <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">

    <context:component-scan base-package="com.test" />
    <context:annotation-config />

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true" />
        <property name="username" value="root"/>
        <property name="password" value=""/>
        <property name="idleTimeout" value="200"/>
        <property name="maximumPoolSize" value="5"/>
        <property name="connectionTimeout" value="30000"/>
        <property name="validationTimeout" value="5000"/>
        <property name="initializationFailFast" value="true"/>
        <property name="maxLifetime" value="1800000"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="test-jpa"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.jdbc.batch_size" value="10"/>
                <entry key="hibernate.order_inserts" value="true"/>
                <entry key="hibernate.order_updates" value="true"/>
                <entry key="hibernate.format_sql" value="true"/>
            </map>
        </property>
        <!--<property name="jpaProperties">-->
            <!---->
        <!--</property>-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>
                <property name="generateDdl" value="false"/>
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <property name="database" value="MYSQL"/>
            </bean>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
        <property name="defaultPersistenceUnitName" value="test-jpa"/>
    </bean>

    <tx:annotation-driven/>

</beans>

这使得每次都可以执行单个查询。我也在相应地刷新和清除实体管理器。

技术栈:Spring 4、Hibernate 5.2。

编辑 1: 我也通过了下面的链接,但没有运气 https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/

编辑 2:

我也关注了下面的链接......但仍然没有运气。

JPA customize the JDBC batch size is not working

datasource-proxy 正在打印以下日志

Name:my-ds, Connection:2, Time:0, Success:True, Type:Prepared, Batch:False, QuerySize:1, BatchSize:0, Query:["insert into student (created_by, created_on, last_modified_on , 版本, 名称) 值 (?, ?, ?, ?, ?)"], Params:[(test,2018-05-13 11:51:29.184,2018-05-13 11:51:29.184,0, Ashok 测试 16)]

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    当您使用IDENTITY 生成器时,Hibernate 不能对 INSERT 语句使用批处理。它只能批处理 UPDATE 和 DELETE 语句。

    尽管如此,切换到TABLE 生成器更糟糕。

    因此,对于 MySQL,批处理 INSERT 语句的唯一方法是使用 JDBC 生成 INSERT 语句。

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 2012-03-31
      • 2017-12-31
      • 1970-01-01
      • 2016-03-17
      • 2020-03-26
      • 2011-02-15
      • 2015-07-03
      相关资源
      最近更新 更多