SpringMvc Jpa mysql开发项目,事务失效,现象:Controller增加和修

10-03-16 zhaome

收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 SpringMvc Jpa mysql开发项目,事务失效,现象:Controller增加和修改无效,但单元测试可以,不知道什么原因,代码如下:

各位高手帮忙解决一下啊,谢谢了。

实体类:

@Entity

public class Customer implements Serializable{

private static final long serialVersionUID = -803075682525328610L;

private Integer customerId;

private String loginName;

private String password="123456";

private String nickName;

private String email;

private String cellphone;

@Id @GeneratedValue

public Integer getCustomerId() {

return customerId;

}

}

DAO:

public interface DAO<T> {

public void save(Object entity);

}

DAO抽象类:

@Transactional

public abstract class DaoSupport<T> implements DAO<T> {

protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());

@PersistenceContext protected EntityManager em;

public void save(Object entity) {

em.persist(entity);

}

}

Service层:

public interface CustomerService extends DAO<Customer>{

}

@Service

@Transactional

public class CustomerServiceBean extends DaoSupport<Customer> implements CustomerService {

}

Controller层:

@Controller

public class ModiPersonDataController {

@Resource CustomerService customerService;

@RequestMapping("/modiPersonDataUI.do")

public String jumpToModiUI() {

return "modiPersonData";

}

/**

* 修改个人资料和密码

*/

@RequestMapping("/modiPersonData.do")

public void modiPersonData(CustomerForm customerForm){

Customer customer = new Customer();

customer.setLoginName(customerForm.getLoginName());

customer.setPassword(customerForm.getPassword());

customer.setNickName(customerForm.getNickName());

customer.setCellphone(customerForm.getCellphone());

customer.setEmail(customerForm.getEmail());

customerService.save(customer);

}

}

webform层:

public class CustomerForm implements Serializable{

private static final long serialVersionUID = -4425312158083346598L;

private Integer customerId;

private String loginName;

private String password;

private String nickName;

private String cellphone;

private String email;

public String getLoginName() {

return loginName;

}

。。。

}

persistence.xml 配置如下:

<?xml version="1.0"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="leo" transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

<property name="hibernate.max_fetch_depth" value="3"/>

<property name="hibernate.hbm2ddl.auto" value="update"/>

<property name="hibernate.jdbc.fetch_size" value="18"/>

<property name="hibernate.jdbc.batch_size" value="10"/>

<property name="hibernate.show_sql" value="true"/>

<property name="hibernate.format_sql" value="false"/>

</properties>

</persistence-unit>

</persistence>

beans.xml配置如下:

<aop:aspectj-autoproxy/>

<context:component-scan base-package="cn.kuaishang"/>

<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${driverClassName}"/>

<property name="url" value="${url}"/>

<property name="username" value="${username}"/>

<property name="password" value="${password}"/>

<property name="initialSize" value="${initialSize}"/>

<property name="maxActive" value="${maxActive}"/>

<property name="maxIdle" value="${maxIdle}"/>

<property name="minIdle" value="${minIdle}"/>

</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />

<property name="loadTimeWeaver">

<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>

</property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

<property name="entityManagerFactory" ref="entityManagerFactory"/>

</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

jdbc.properties)(略)

spring mvc 配置如下:

<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->

<context:component-scan base-package="cn.kuaishang"/>

<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

web.xml 配置如下:

<display-name>Spring Annotation MVC Sample</display-name>

<!-- Spring 服务层的配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:beans.xml</param-value>

</context-param>

<!-- Spring 容器启动监听器 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的

配置文件,以启动Spring MVC模块-->

<servlet>

<servlet-name>annomvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>annomvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

[该贴被zhaome于2010-03-16 16:36修改过]

zhaome
2010-03-16 16:38

顶一下,在线等待您的回复。

zhaome
2010-03-16 20:53

第一次在这儿发帖,居然没人帮忙,真冷清啊。

banq
2010-03-17 10:37

贴一大堆代码上来,谁有时间帮你检查代码啊,自己搞断点调试吧,在事务失败发生之前看断点研究吧。

代码调试应该是程序员基本技能。

如果有设计问题,可贴一些图上来表达,不好意思,帮不了你。

160649888
2010-03-17 19:19

原因是:事务没有起动

2Go 1 2 下一页