最近学习spring mvc,用到jpa简化DAO层代码,发现save死活不触发SQL语句,找了很久才解决这个问题,实在是坑。、mysql
<!-- 关键是这个bean,必定要设置正确才行 --> <bean id="transactionManager">
二话不说了,直接贴配置文件:spring
<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就不行了。终于解决了,下次研究一下二者有什么不一样。sql
(未完待续…………)apache