利用Spring的事务管理无法将数据写到数据库中啊
下面是我的代码:
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
default-autowire="byName" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.github.Yasenia.flea_school.server">
</context:component-scan>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fleaSchool" />
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="jpaDialect"
class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</beans>
springMVC-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" default-autowire="byName"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.github.Yasenia.flea_school.server">
</context:component-scan>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 对静态资源文件的访问 方案 -->
<mvc:default-servlet-handler />
</beans>
IUserDAOImpl.java
@Named
public class UserDAOImpl implements IUserDAO {
@PersistenceContext
private EntityManager em;
public void save(User user) {
em.persist(user);
}
...
}
IUserService.java:
@Named
public class UserServiceImpl implements IUserService {
@Inject
private IUserDAO userDAO;
@Transactional(propagation = Propagation.REQUIRED)
public void save(User user) {
user.setRegisterDate(new Date());
userDAO.save(user);
}
}
我手动开启和提交事务也不行
public void save(User user) {
System.out.println("userDAO working");
EntityTransaction transaction = em.getTransaction();
transaction.begin();
System.out.println("userDAO saving");
System.out.println("em is: " + em);
em.persist(user);
transaction.commit();
}
执行到EntityTransaction transaction = em.getTransaction();这句话就会退出方法。
不知道是什么原因,求大神指点啊!
------解决方案--------------------