我正在使用 Hibernate 4.1.0.Final、JPA 2.0、JUnit 4.8.1 和 H2 内存数据库。我有这个 JPA 查询...
@Override
public List<Organization> findByOrgTypesCountryAndState(final List<OrganizationType> organizationTypes,
final String countryId,
final String stateId)
{
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Organization> criteria = builder.createQuery(Organization.class);
final Root<Organization> org = criteria.from(Organization.class);
criteria.select(org);
criteria.where(builder.and(
org.get("organizationType").in(organizationTypes),
builder.equal(org.get("state").get("id"), stateId),
builder.equal(org.get("country").get("id"), countryId)));
return entityManager.createQuery(criteria).getResultList();
}
当 java.util.List、organizationTypes 为空时,上述查询将终止,并出现以下异常。我应该使用另一种谓词来解释这种情况吗?
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [select generatedAlias0 from org.mainco.subco.organization.domain.Organization as generatedAlias0 where ( generatedAlias0.organizationType in () ) and ( generatedAlias0.state.id=:param0 ) and ( generatedAlias0.country.id=:param1 )]" type="java.lang.IllegalArgumentException">java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [select generatedAlias0 from org.mainco.subco.organization.domain.Organization as generatedAlias0 where ( generatedAlias0.organizationType in () ) and ( generatedAlias0.state.id=:param0 ) and ( generatedAlias0.country.id=:param1 )]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1347)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:506)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:220)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:623)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at com.sun.proxy.$Proxy51.createQuery(Unknown Source)
at org.mainco.subco.organization.repo.OrganizationDaoImpl.findByOrgTypesCountryAndState(OrganizationDaoImpl.java: 232)
at org.mainco.subco.organization.repo.OrganizationDaoTest.testFindByOTCSEmptyOrgTypeList(OrganizationDaoTest.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)