我使用 Spring、Hibernate 和 MySQL 开发应用程序。我为我的一张表创建了 DAO。现在,我想测试 DAO 的方法(保存、查找等)。问题是这些方法会影响数据库数据,所以我想在特定方法执行后回滚所有更改。我尝试通过设置defaultRollback=true
来做到这一点,@TransactionConfiguration
但它不起作用。下面我粘贴最重要的代码片段。有谁知道如何在每种方法之后强制回滚?
我在 MySQL 中的表使用 InnoDB 引擎。事实上,在测试执行后控制台包含这样的信息:
INFO: Rolled back transaction after test execution for test context ...
但是数据库中的更改被提交了。
用户DAOTest
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class UsersDAOTest {
@Autowired
UsersHibernateDAO usersDAO;
@Test
public void test1() {
List<Users> results = usersDAO.findAll();
Assert.assertEquals(0, results.size());
}
@Test
public void test2() {
Users user = new Users("mchrobok", "12345678901234567890123456789012");
usersDAO.saveOrUpdate(user);
List<Users> results = usersDAO.findAll();
Assert.assertEquals(1, results.size());
}
}
休眠 (hibernate.cfg.xml)
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping class="pl.fp.microblog.domain.Users"/>
</session-factory>
</hibernate-configuration>
弹簧配置(applicationContext.xml)
<beans>
<context:component-scan base-package="pl.fp.microblog" />
<tx:annotation-driven />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mysql://localhost:3306/Microblog"/>
<beans:property name="username" value="root"/>
<beans:property name="password" value="root"/>
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation">
<beans:value>
hibernate.cfg.xml
</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<beans:bean name="usersDAO" class="pl.fp.microblog.dao.UsersHibernateDAO">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
</beans:beans>
编辑
UsersDAO(它是通用的,但没关系)
public class GenericHibernateDAO<T> implements GenericDAO<T> {
private SessionFactory sessionFactory;
private Class<?> persistClass;
public GenericHibernateDAO() {
ParameterizedType type = ((ParameterizedType)getClass().getGenericSuperclass());
persistClass = ((Class<?>) type.getActualTypeArguments()[0]);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<T> findAll() {
Criteria criteria = sessionFactory.openSession().createCriteria(persistClass);
return criteria.list();
}
@Override
public void saveOrUpdate(T entity) {
sessionFactory.openSession().saveOrUpdate(entity);
}
}