您好,我在某些JUnit测试中遇到了Spring Hibernate会话管理的问题。
我有一个签名为@Transactional的测试类,以便为每个测试方法提供autorollback功能;在测试期间,我将三行插入数据库,然后运行本机查询(使用session.createSQLQuery()),该查询应返回包含我刚刚保存的所有三个元素的列表,但本机查询的结果始终是空。
我试图从测试类中删除@Transactional,因此插入过程将以提交结束,在这种情况下,我的本机查询工作正常,测试为绿色但没有执行回滚(由于缺少@Transactional)。
就我个人而言,我宁愿不用@Rollback签名测试(false)并手动删除数据,所以我想知道这种奇怪的行为是否是由不正确的配置引起的。
提前致谢。
在我的Spring会话和事务管理器配置下面,测试中的有罪测试和本机查询。
Spring Config:
@Bean(name = "sessionFactory")
@Autowired
public SessionFactory getSessionFactory(final DataSource dataSource) throws Exception {
final LocalSessionFactoryBuilder springSessionFactoryBean = new LocalSessionFactoryBuilder(dataSource);
springSessionFactoryBean.addAnnotatedClasses(CvSentEvent.class);
final Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
hibernateProperties.put("hibernate.cache.use_second_level_cache", true);
hibernateProperties.put("hibernate.hbm2ddl.auto", "validate");
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.put("hibernate.format_sql", false);
hibernateProperties.put("hibernate.jdbc.use_scrollable_resultsets", true);
hibernateProperties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
hibernateProperties.put("org.hibernate.cache.ehcache.configurationResourceName", "ehcache.xml");
springSessionFactoryBean.addProperties(hibernateProperties);
return springSessionFactoryBean.buildSessionFactory();
}
测试中的方法
@Override
@Transactional(readOnly = true)
public List<CvSentReport> searchByWebsiteAccount(final String website, final String account, final String cvs, final String budgetFIlter) {
final String sql = ""
+ "select sent, website, account from (select "
+ " sum(c.count) as sent, "
+ " c.website, "
+ " c.sponsoraccountname as account "
+ " from "
+ " cvsentevents c "
+ " where "
+ " c.website like :website "
+ " and (c.sponsoraccountname like :account or (c.sponsoraccountname IS NULL and :account = '%%')) "
+ " group by "
+ " c.website, "
+ " c.sponsoraccountname ) z " + filterForCVs(cvs) + ";";
final List<Object> list = new ArrayList<Object>();
@SuppressWarnings("unchecked")
final List<Object> itemList = getSession()
.createSQLQuery(sql)
.setParameter("website", "%" + website + "%")
.setParameter("account", "%" + account + "%")
.list();
list.addAll(itemList);
return createAListOfCvSentReports(budgetFIlter, list);
}
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringJunitContext.class })
@Transactional
public class CvSentEventDaoTest {
@Autowired
private CvSentEventDao sut;
@Test
public void findAllTheItems() {
newCvSentEvent(0, "website1", "account1", "1");
newCvSentEvent(1, "website2", "account2", "1");
newCvSentEvent(1, "website3", "account3", "0");
final List<CvSentReport> actual = sut.searchByWebsiteAccount("website", "account", "all", "all");
assertEquals(3, actual.size());
}
}
答案 0 :(得分:0)
有过类似的问题。在事务测试方法调用期间,实体无法从mem中的H2中删除。修复了在删除DAO方法中刷新当前会话的问题。