SSH框架整合入门helloworld两种事务管理方式(失败的自己)
?
?
?
applicationContext.xml:注解事务管理,需要在方法上加@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
?
?
xml文件配置的事务管理方式:这种方式不需要在方法上加@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
?
?
?
?
?
struts.xml:
?
?
?
?
action:
?
?
?
?
?
?
?
?
实体类:Priority.java?
?
?
?
?
?User.java?
?
?映射文件:(没有配置表关联关系):
?User.hbm.xml
?
?
?
?
editUser.jsp
?
?
log4j.properties:
?
package com.tch.test.ssh.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.tch.test.ssh.entity.Priority;import com.tch.test.ssh.entity.User;public class SpringTest {@Testpublic void testHibernate(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");Session session = sessionFactory.openSession();session.beginTransaction();User u = new User();u.setId(1);u = (User)session.get(User.class, 1);System.out.println(u.getPriorities());Priority p = (Priority) session.load(Priority.class, 3);u.getPriorities().add(p);//u.getPriorities().remove(p);//Priority p = new Priority();//p.setId(3);//u.setPriorities(null);session.update(u);System.out.println(u.getPriorities());session.getTransaction().commit();session.close();}}?
?
?
?
?
完整代码:网盘下载