我正在Spring 3.1.2
使用Hibernate 4
.
我有一个MyDaoImpl
带有注释的 DAO 实现类,@Repository
以便启用异常翻译。我有一个MyService
注释@Transactional
如下的服务类:
public class MyDaoImpl implements MyDao {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void saveA(A a)
{
this.sessionFactory.getCurrentSession().saveOrUpdate(a);
}
}
我编写了一个单元测试类MyDaoImplTest
,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:beans.xml" })
@Transactional
public class MyDaoImplTest implements IMyDaoImplTest {
private MyDao myDao;
private SessionFactory sessionFactory;
@Autowired
public void setMyDao(MyDao myDao)
{
this.myDao = myDao;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Test
@Override
public void testCreateA()
{
A a = new A("A1");
this.myDao.saveA(a);
this.sessionFactory.getCurrentSession().flush();
IDataSet databaseDataSet = this.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("Applications");
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(this.getClass().getClassLoader().getResourceAsStream("test-data/applications/savenew.xml"));
ITable expectedTable = expectedDataSet.getTable("Applications");
Assertion.assertEquals(expectedTable, actualTable);
}
}
根据 Spring 文档,我在更改后刷新会话以避免误报。问题是刷新后,如果我在 Hibernate 之外访问数据库(例如使用 DBUnit),我看不到更改,所以我的断言总是失败。
我的配置有什么问题?