最近学习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就不行了。终于解决了,下次研究一下两者有什么不同。

(未完待续…………)

spring-data-jpa中save不触发数据库insert语句的问题的更多相关文章

  1. Spring data JPA中使用Specifications动态构建查询

    有时我们在查询某个实体的时候,给定的条件是不固定的,这是我们就需要动态 构建相应的查询语句,在JPA2.0中我们可以通过Criteria接口查询,JPA criteria查询.相比JPQL,其优势是类 ...

  2. Spring Data JPA中的动态查询 时间日期

    功能:Spring Data JPA中的动态查询 实现日期查询 页面对应的dto类private String modifiedDate; //实体类 @LastModifiedDate protec ...

  3. 【hql】spring data jpa中 @Query使用hql查询 问题

    spring data jpa中 @Query使用hql查询 问题 使用hql查询, 1.from后面跟的是实体类 不是数据表名 2.字段应该用实体类中的字段 而不是数据表中的属性 实体如下 hql使 ...

  4. Spring Data JPA中CrudRepository与JpaRepository的不同

    使用Spring Data JPA CrudRepository 和JpaRepository 的好处: 继承这些接口,可以使Spring找到自定义的数据库操作接口,并生成代理类,后续可以注入到Spr ...

  5. Spring data jpa中Query和@Query分别返回map结果集

    引用: http://blog.csdn.net/yingxiake/article/details/51016234 http://blog.csdn.net/yingxiake/article/d ...

  6. 在Spring Data JPA 中使用Update Query更新实体类

    对于 Spring Data JPA 使用的时间不长,只有两年时间.但是踩过坑的却不少. 使用下列代码 @Modifying @Query("update User u set u.firs ...

  7. 如何在Spring Data JPA中引入Querydsl

    一.环境说明 基础框架采用Spring Boot.Spring Data JPA.Hibernate.在动态查询中,有一种方式是采用Querydsl的方式. 二.具体配置 1.在pom.xml中,引入 ...

  8. Spring Data JPA 中常用注解

    一.java对象与数据库字段转化 1.@Entity:标识实体类是JPA实体,告诉JPA在程序运行时生成实体类对应表 2.@Table:设置实体类在数据库所对应的表名 3.@Id:标识类里所在变量为主 ...

  9. 在spring data jpa中使用自定义转换器之使用枚举转换

    转载请注明http://www.cnblogs.com/majianming/p/8553217.html 在项目中,经常会出现这样的情况,一个实体的字段名是枚举类型的 我们在把它存放到数据库中是需要 ...

随机推荐

  1. Web开发——Photoshop(PSD格式截取)

    Step1 将截取到的图片,存储为Web所用格式. Step2 在右上角选择存储格式为:PNG-24(PNG-8可能会出现白边).

  2. oracle学习笔记第二天

    一.连接查询 --笛卡尔积(表 * 表),连接的基础select * from emp,dept;--等值连接select * from emp e,dept d where e.deptno = d ...

  3. 浅谈最长上升子序列(LIS)

    一.瞎扯的内容 给一个长度为n的序列,求它的最长上升子序列(LIS) 简单的dp n=read(); ;i<=n;i++) a[i]=read(); ;i<=n;i++) ;j<i; ...

  4. 批量将PowerDesigner中表字段由小写变成大写

    通过以下VB脚本即可批量修改,在Tools=>Execute Commands下的Edit/Run Scripts,或者通过Ctrl+Shift+X运行以下脚本即可: '************ ...

  5. ThinkPHP 5 验证码

    <div> <form action="{:url('index/check')}" method="post"> <img sr ...

  6. webpack 4.0 配置文件 webpack.config.js文件的放置位置

    一般webpack.config.js是默认放在根目录的,不在根目录的时候需要在package.json中制定位置,我的配置文件目录是config/webpack.config.js,在package ...

  7. ORACLE——EXTRACT() 截取日期时间的函数使用

    1.截取日期的 年: --使用方法 EXTRACT(YEAR FROM DATE) SQL> SELECT EXTRACT( YEAR FROM SYSDATE ) FROM DUAL; --结 ...

  8. AIX stack_hard参数

    OS:AIX 7.1 root.oracle.grid用户查看资源限制,其中stack受限,需要在/etc/security/limits中增加stack_hard = -1,受硬限制限制! [ora ...

  9. 灵雀云:etcd 集群运维实践

    [编者的话]etcd 是 Kubernetes 集群的数据核心,最严重的情况是,当 etcd 出问题彻底无法恢复的时候,解决问题的办法可能只有重新搭建一个环境.因此围绕 etcd 相关的运维知识就比较 ...

  10. 解决postman环境切换,自动获取api签名时间及签名

    postman调试api接口时,常遇到两个问题: 1.环境分为开发环境,测试环境,正式环境,如何只写一个接口,通过切换postman环境来实现不同环境的接口调用? 2. api接口请求时往往会添加,来 ...