我正在使用H2嵌入式数据库实现单元测试。当我在表中插入一个条目时,我能够使用本机SQL查询检索该条目,但不能使用HQL查询。主键查找也可以通过JPA EntityManager接口工作。我使用Hibernate作为持久性提供程序。
配置:
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true" p:generateDdl="false" p:databasePlatform="org.hibernate.dialect.H2Dialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter"
p:persistenceUnitName="AMgSAdapter" p:packagesToScan="com.hcsc.amgs.adapter.domaindto">
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.ejb.interceptor" value="com.hcsc.amgs.adapter.dao.hibernate.interceptor.AuditableFieldInterceptor" />
</map>
</property>
</bean>
测试用例:
@Test
@Transactional(value="transactionManager",propagation = Propagation.REQUIRED)
public void getVasPolicyMatchesTest() {
Application application = TestDataCreationUtil.createApplication();
entityManager.persist(application.getX834EnrollmentApplicationTransaction());
entityManager.persist(application);
List<Application> matches = new ArrayList<Application>();
Query q = simpleQueryHQL();
matches= q.getResultList();
Application findApp = entityManager.find(Application.class, application.getId()); // this works
Assert.assertTrue(matches.size() > 0);
}
private Query simpleQueryHQL() {
return entityManager.createQuery("Select app From Application app", Application.class);
}
private Query simpleQuerySQL() {
return entityManager.createNativeQuery("Select * from Application", Application.class);
}
没有抛出异常,simpleQueryHQL()的测试用例失败,而simpleQuerySQL()传递