【发布时间】: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