spring-data-jpa中save不触发数据库insert语句的问题

本文详细介绍了在SpringMVC项目中使用JPA时遇到的问题及解决方案,特别是关于事务管理器配置不当导致的save方法不触发SQL语句的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近学习spring mvc,用到jpa简化DAO层代码,发现save死活不触发SQL语句,找了好久才解决这个问题,实在是坑。、

<!-- 关键是这个bean,一定要设置正确才行 -->
<bean id="transactionManager">

 

二话不说了,直接贴配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-4.0.xsd" >  
 
        <context:annotation-config/>
        <context:component-scan base-package="edu.zipcloud.cloudstreetmarket.core" />  
      
        <jpa:repositories base-package="edu.zipcloud.cloudstreetmarket.core.daos" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property> 
        <property name="url">
            <value>jdbc:mysql://localhost:3306/testdb</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
    </bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="jpaData"/>
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                 <prop key="hibernate.hbm2ddl.auto">update</prop> 
                <!--  <prop key="hibernate.hbm2ddl.auto">create</prop> -->
                <!-- <prop key="hibernate.hbm2ddl.auto">create-drop</prop>  -->
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.default_schema">testdb</prop>
            </props>
        </property>
</bean>

    <!-- 密码编码器,如果不加这个,spring不知道要用哪一个 -->
    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

    <!-- 当容器启动时,执行SQL -->
    <jdbc:initialize-database data-source="dataSource" enabled="false">
        <jdbc:script location="classpath:/META-INF/db/init.sql"/>
    </jdbc:initialize-database>
    
    <!-- 事务 就是这里有问题啦 --> 
    <!--  Since you're using JPA, 
        the transaction manager should be a JpaTransactionManager, 
        not a DataSourceTransactionManager.  -->
    <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>   --> 
    <!-- fuck off !!! -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />
</beans>

stackoverflow上找到了原因,就是这个JPA,有一个专用的事务管理器,org.springframework.jdbc.datasource.DataSourceTransactionManager,如果用DataSourceTransactionManager就不行了。终于解决了,下次研究一下两者有什么不同。

(未完待续…………)

转载于:https://www.cnblogs.com/myjhaha/p/9597461.html

<think>嗯,用户想了解Spring Data JPA的批量新增实现原理,以及为什么看起来像是在循环中逐条插入。首先,我需要回忆一下Spring Data JPA的工作机制。记得JPA本身有EntityManager来处理持久化操作,而Spring Data JPA在它的基础上做了很多封装,简化了开发。 用户提到批量新增可能像是在循环中逐条插入,这可能涉及到Hibernate的缓存机制。Hibernate默认情况下可能会自动进行批量插入,而是逐条处理。这可能是因为没有正确配置批量操作的参数,比如hibernate.jdbc.batch_size。如果没有设置这个参数,Hibernate可能会启用批量处理,导致每次插入都单独执行SQL语句。 然后,我需要看看saveAll()方法的源码。Spring Data JPA的CrudRepository接口中的saveAll()方法默认实现应该是循环调用save()方法。这可能导致每个实体都被单独处理,生成单独的INSERT语句。这时候如果没有开启批处理,每次插入都会单独执行,效率自然高。 过,JPA规范中有没有关于批量操作的支持呢?我记得JPA 2.1引入了StoredProcedureQuery,但批量插入主要还是依靠Hibernate的批量处理功能。需要检查是否启用了JDBC批处理,以及事务的管理是否正确。因为在事务外,每个save()可能会立即执行,而无法批量处理。 另外,级联操作和主键生成策略也会影响批量插入。如果使用自增主键,Hibernate可能无法进行批量优化,因为它需要立即获取生成的主键值。使用GenerationType.IDENTITY时会禁用批处理,而SEQUENCE或TABLE则可能支持。 用户可能遇到的问题没有正确配置Hibernate的批处理参数,或者在保存数据时没有在一个事务中处理,导致每次插入都是单独的。需要建议他们检查配置,确保hibernate.jdbc.batch_size设置合理,并且在事务中执行批量保存,同时考虑主键生成策略的影响。 最后,可能还需要提到一些优化方法,比如手动分块处理数据,使用EntityManager的persist()方法,并在适当的时候刷新和清除缓存,以避免内存溢出。这样用户可以根据实际情况调整代码,提升批量插入的效率。</think>### Spring Data JPA批量新增实现原理分析 #### 1. 核心机制 Spring Data JPA的批量新增依赖于**Hibernate的批量操作机制**。默认情况下,调用`saveAll()`方法时,Hibernate会通过`Session`的缓存机制管理实体状态,但**未显式启用批处理时,会逐条生成SQL语句并执行**[^1]。关键点包括: - **JDBC批处理配置**:需通过`hibernate.jdbc.batch_size`参数显式设置批处理大小(如设置为100),Hibernate会将多个INSERT语句合并为一批发送到数据库[^2]。 - **事务边界**:批量操作必须包裹在单个事务中,否则每条INSERT会独立提交,导致批处理失效。 #### 2. 源码解析 以`SimpleJpaRepository`的`saveAll()`实现为例: ```java public <S extends T> List<S> saveAll(Iterable<S> entities) { List<S> result = new ArrayList<>(); for (S entity : entities) { result.add(save(entity)); // 循环调用save() } return result; } ``` 此处表面上是循环插入,但实际行为取决于: - **Hibernate的批处理配置** - **主键生成策略**(如`GenerationType.IDENTITY`会禁用批处理) - **事务上下文** #### 3. 逐条插入现象原因 以下情况会导致实际逐条执行: 1. **未配置批处理参数**:缺少`hibernate.jdbc.batch_size`设置 2. **使用自增主键**:`@GeneratedValue(strategy = IDENTITY)`要求逐条获取主键 3. **事务未统一管理**:多次`save()`调用分散在同事务中 4. **混合读写操作**:批处理过程中穿插查询会触发缓存刷新 #### 4. 性能优化方案 | 优化手段 | 实现方式 | |---------------------------|--------------------------------------------------------------------------| | 启用JDBC批处理 | 配置`spring.jpa.properties.hibernate.jdbc.batch_size=100` | | 使用SEQUENCE主键 | `@GeneratedValue(strategy = SEQUENCE)` | | 手动分块提交 | 每N条数据执行`flush()`和`clear()` | | 禁用二级缓存 | 批处理期间设置`hibernate.cache.use_second_level_cache=false` | #### 5. 高效批量插入示例 ```java @Transactional public void batchInsert(List<Entity> list) { for (int i = 0; i < list.size(); i++) { entityManager.persist(list.get(i)); if (i % 100 == 0) { // 按批处理大小分块 entityManager.flush(); entityManager.clear(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值