0
我不明白为什么Spring @Transactional
测试方法Hibernate没有回滚在@Before
中所做的更改?有information,@Before
和@Test
在一个事务中被调用。所有上下文都可以考虑配置。用`.createSQLQuery`截断后弹簧/休眠不回滚事务
@Test
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional(defaultRollback = true, transactionManager = "transactionManager")
public class TestClass {
@Autowired
private SessionFactory sessionFactory;
@Autowired
private EntityDao entityDao;
@Before
public void before() {
// create arbitrary entity
Entity one = Utils.createEntity();
// save with HibernateDao to table_1
entityDao.save(one);
}
@Test
public void test() {
sessionFactory.getCurrentSession().createSQLQuery("TRUNCATE table_2").executeUpdate();
}
}
没有createSQLQuery().executeUpdate
如需要@Before
所有的改变都rollbacked。
哪些DBMS您使用的?并非所有人都支持事务TRUNCATE –
我使用的是mysql,据说['Truncate操作导致隐式提交,因此无法回滚](https://dev.mysql.com/doc/refman/ 5.7/EN /截短table.html)。你能通过手势来解释为什么有些dbms不支持事务截断吗?我想知道在这种情况下会发生什么。 –