Java Spring4+Hibernate4+Junit@Transactional nativeSQL查询
嗨,我在一些JUnit测试中遇到了Spring Hibernate会话管理问题 我有一个签名为@Transactional的测试类,以便为每个测试方法提供自动回滚功能;在测试期间,我将三行插入数据库,然后使用session.createSQLQuery运行本机查询,该查询应返回一个包含我刚才保存的所有三个元素的列表,但本机查询的结果始终为空 我已经尝试从测试类中删除@Transactional,这样插入过程将以提交结束,在这种情况下,我的本机查询工作得很好,测试是绿色的,但由于缺少@Transactional,因此没有执行回滚 就我个人而言,我不希望用@rollbackalse对测试进行签名并手动删除数据,所以我想知道这种奇怪的行为是否是由错误的配置引起的 提前谢谢 下面是我的Spring会话和事务管理器配置,包含测试和正在测试的本机查询 弹簧配置:Java Spring4+Hibernate4+Junit@Transactional nativeSQL查询,java,spring,hibernate,junit,spring-orm,Java,Spring,Hibernate,Junit,Spring Orm,嗨,我在一些JUnit测试中遇到了Spring Hibernate会话管理问题 我有一个签名为@Transactional的测试类,以便为每个测试方法提供自动回滚功能;在测试期间,我将三行插入数据库,然后使用session.createSQLQuery运行本机查询,该查询应返回一个包含我刚才保存的所有三个元素的列表,但本机查询的结果始终为空 我已经尝试从测试类中删除@Transactional,这样插入过程将以提交结束,在这种情况下,我的本机查询工作得很好,测试是绿色的,但由于缺少@Transa
@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());
}
}
也有类似的问题。在事务测试方法调用期间,无法从mem中的H2中删除实体。修复了在删除DAO方法中刷新当前会话的问题。将SessionFactory注入测试用例中,在搜索之前执行getCurrentSession.flush。正确设置参数并计算其类型,如果查询必须返回某些内容。我不明白,为什么要测试Hibernate?正确编写sql的测试地点是数据库,而不是java junit测试。