package cn.edu.hpu.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class User { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package cn.edu.hpu.dao.Impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; @Component("u") public class UserDaoImpl implements UserDao{ private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void save(User u) { Session s=sessionFactory.openSession(); s.beginTransaction(); s.save(u); s.getTransaction().commit(); System.out.println("add success!!"); } }
package cn.edu.hpu.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_log") public class Log { private int id; private String msg; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
package cn.edu.hpu.dao.Impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import cn.edu.hpu.dao.LogDao; import cn.edu.hpu.model.Log; @Component("logDao") public class LogDaoImpl implements LogDao{ private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void save(Log log) { Session s=sessionFactory.openSession(); s.beginTransaction(); s.save(log); s.getTransaction().commit(); System.out.println("Log add success!!"); } }
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>cn.edu.hpu.model.User</value> <value>cn.edu.hpu.model.Log</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
package cn.edu.hpu.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import cn.edu.hpu.dao.LogDao; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.Log; import cn.edu.hpu.model.User; @Component("userService") public class UserService { private UserDao userDao; private LogDao logDao; public UserDao getUserDao() { return userDao; } @Resource(name="u") public void setUserDao(UserDao userDao) { this.userDao = userDao; } public LogDao getLogDao() { return logDao; } @Resource(name="logDao") public void setLogDao(LogDao logDao) { this.logDao = logDao; } public void add(User u){ this.userDao.save(u); Log log=new Log(); log.setMsg("a user saved!"); logDao.save(log); } }
package cn.edu.hpu.service; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception{ ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); System.out.println(userService.getClass()); User u=new User(); u.setName("jack"); userService.add(u); ctx.destroy(); } }
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"> </bean>
<tx:annotation-driven transaction-manager="txManager"/>
iv.在需要事务的方法上加:@Transactional
@Transactional public void add(User u){ this.userDao.save(u); Log log=new Log(); log.setMsg("a user saved!"); this.logDao.save(log); }它会自动在前后帮你加上关于业务的事务逻辑
public void save(User u) { Session s=sessionFactory.getCurrentSession(); s.save(u); System.out.println("add success!!"); } public void save(Log log) { Session s=sessionFactory.openSession(); s.save(log); System.out.println("Log add success!!"); }v.需要注意,使用SessionFactory.getCurrentSession 不要使用OpenSession
package cn.edu.hpu.service; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception{ ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); User u=new User(); u.setName("jack"); userService.add(u); ctx.destroy(); } }测试结果:
public void save(Log log) { Session s=sessionFactory.openSession(); s.save(log); throw new RuntimeException("Error"); }
@Test public void testAdd() throws Exception{ ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); User u=new User(); u.setName("jack"); userService.add(u); ctx.destroy(); }结果:
意思是在当前的这个环境中,别人调我这个方法,比如说已经有了一个Transaction了,就用原来那个,把自己作为Transaction的一部分加进去就行了,如果没有就创建一个新的。
转载请注明出处:http://blog.csdn.net/acmman/article/details/44651921