当前位置: 首页 > 知识库问答 >
问题:

Hibernate批处理插入,Spring和Hibernate不起作用

许昆
2023-03-14

我很难让Hibernate在MySQL上执行大容量插入。

我有要排序的主键生成类型,下面是我的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自定义JDBC批量大小不起作用

数据源代理正在打印以下日志

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,version,Name)值(?, ?, ?, ?, ?)"],Params:[(测试,2018-05-13 11:51:29.184,2018-05-13 11:51:29.184,0,Ashok测试16)]

共有1个答案

商德泽
2023-03-14

当您使用标识生成器时,Hibernate不能对INSERT语句使用批处理。它只能批量更新和删除语句。

然而,切换到< code>TABLE生成器更糟糕。

因此,对于MySQL,批量插入语句的唯一方法是使用JDBC生成插入语句。

 类似资料:
  • 我正在使用hibernate jpa执行批处理更新。 更新:我得到了解决方案:问题是我正在刷新已经刷新的事务,因此它没有给我任何事务正在进行中的错误以及我上面发布的错误,所以我只是删除了`getem().flush();和getEm().clear();从finally块开始工作:)

  • 我需要在我的MySQL数据库中进行批量插入(近10000个)。我正在使用JPA/Hibernate和spring Boot。我从hibernate文档中读到了在hibernate中执行大容量插入/更新,我认为我的代码不能正常工作,因为它顺序地插入hibernate查询,而不是在批处理插入中执行它们。下面是我的代码。我是不是漏掉了什么? 这是我的数据源配置。 我是不是漏掉了什么? 请帮帮忙。

  • 问题内容: 有没有解决方案通过hibernate在分区后的PostgreSQL表中批量插入?目前我遇到这样的错误… 我已经找到此链接http://lists.jboss.org/pipermail/hibernate- dev/2007-October/002771.html, 但我在网上找不到任何地方可以解决此问题或如何解决该问题 问题答案: 您可能想通过设置hibernate.jdbc.fac

  • 问题内容: 我有一个数据模型,该数据模型在一个实体和其他11个实体之间具有一对多关系。这12个实体一起代表一个数据包。我遇到的问题是与这些关系的“许多”方面发生的插入次数有关。其中一些可以具有多达100个单独的值,因此要将一个完整的数据包保存在数据库中,最多需要500次插入。 我正在将MySQL 5.5与InnoDB表一起使用。现在,通过测试数据库,我发现在处理批量插入时,它每秒可以轻松地每秒进行

  • 出于不需要透露的原因,我需要使用hibernate层运行一系列本机SQL语句。它们是相同的语句(相同的绑定变量),形式为“插入abc(column1,column2)值(:Column1List,:Column2List)”。

  • 我有一个非常恼人的问题,我已经在网上阅读了所有现有的文档,并阅读了所有与此主题相关的问题和答案,但根本无法使其发挥作用! 我真的很绝望,我不知道我错过了什么,所以我会努力给你我所拥有的一切。基本上,我试图做的是用一个查询保存大量数据,而不是每个对象的多个查询。你可以怀疑我正在使用Spring Boot, Hibernate和MySql。 因此,根据我读到的与“使用mysql hibernate进行