我正在使用Spring 3.1.1.RELEASE,Hibernate 4.1.0.Final,JUnit 4.8和JPA 2.0(hibernate-jpa-2.0-api)。我正在尝试根据成员字段的字段编写查询和搜索。我的意思是我有这个实体......
@GenericGenerator(name = "uuid-strategy", strategy = "uuid.hex")
@Entity
@Table(name = "cb_organization", uniqueConstraints = {@UniqueConstraint(columnNames={"organization_id"})})
public class Organization implements Serializable
{
@Id
@NotNull
@GeneratedValue(generator = "uuid-strategy")
@Column(name = "id")
/* the database id of the Organization */
private String id;
@ManyToOne
@JoinColumn(name = "state_id", nullable = true, updatable = false)
/* the State for the organization */
private State state;
@ManyToOne
@JoinColumn(name = "country_id", nullable = false, updatable = false)
/* The country the Organization is in */
private Country country;
@ManyToOne(optional = false)
@JoinColumn(name = "organization_type_id", nullable = false, updatable = false)
/* The type of the Organization */
private OrganizationType organizationType;
注意成员“organizationType”,“state”和“country”,它们都是对象。我希望根据他们的id字段构建一个查询。这段代码
@Override
public List<Organization> findByOrgTypesCountryAndState(Set<String> organizationTypes,
String countryId,
String stateId)
{
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Organization> criteria = builder.createQuery(Organization.class);
Root<Organization> org = criteria.from(Organization.class);
criteria.select(org).where(builder.and(org.get("organizationType.id").in(organizationTypes),
builder.equal(org.get("state.id"), stateId),
builder.equal(org.get("country.id"), countryId)));
return entityManager.createQuery(criteria).getResultList();
}
正在抛出以下异常。我该如何治愈疼痛?
java.lang.IllegalArgumentException: Unable to resolve attribute [organizationType.id] against path
at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:116)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:221)
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:192)
at org.mainco.subco.organization.repo.OrganizationDaoImpl.findByOrgTypesCountryAndState(OrganizationDaoImpl.java:248)
at org.mainco.subco.organization.repo.OrganizationDaoTest.testFindByOrgTypesCountryAndState(OrganizationDaoTest.java:55)
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)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
答案 0 :(得分:16)
JPA Criteria building未定义对路径表达式的支持。正确的标准是:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Organization> criteria = builder.createQuery(Organization.class);
Root<Organization> org = criteria.from(Organization.class);
criteria.select(org);
criteria.where(
builder.and(
org.get( "organizationType" ).get( "id" ).in( organizationTypes ),
builder.equal( org.get( "state" ).get( "id" ), stateId ),
builder.equal( org.get( "country" ).get( "id" ), countryId )
)
);