我有这个规格:
public static Specification<CompensationCase> containsClaimantName(final String firstName, final String middleName, final String surName, final String motherMaidenName) {
return new Specification<CompensationCase>() {
public Predicate toPredicate(Root<CompensationCase> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Path<Claimant> claimantPath = root.get(CompensationCase_.claimant);
Subquery<ClaimantPerson> claimantPersonSubquery = query.subquery(ClaimantPerson.class);
Root<ClaimantPerson> claimantPersonRoot = claimantPersonSubquery.from(ClaimantPerson.class);
claimantPersonSubquery.
select(claimantPersonRoot).
where(
ClaimantPersonSpecs
.containsPersonName(firstName, middleName, surName, motherMaidenName)
.toPredicate(claimantPersonRoot, query, cb));
return cb.in(claimantPath.as(ClaimantPerson.class)).value(claimantPersonSubquery);
}
};
}
其中
ClaimantPerson
从Claimant
继承。我读到您可以使用.as()
进行转换,但是不确定是否执行正确。目前,我收到此错误:--Erroring: batchId[2] message[java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '.' near line 1, column 137 [select generatedAlias0 from test.project.compensation.model.CompensationCase as generatedAlias0 where cast(generatedAlias0.claimant as test.project.compensation.model.ClaimantPerson) in (select generatedAlias1 from test.project.compensation.model.ClaimantPerson as generatedAlias1 where generatedAlias1.personRef in (select generatedAlias2 from test.project.entity.identity.PersonRef as generatedAlias2 where ( ( ( lower(generatedAlias2.name.firstName) like :param0 ) and ( 1=1 ) ) and ( lower(generatedAlias2.name.surname) like :param1 ) ) and ( lower(generatedAlias2.name.motherMaidenName) like :param2 )))]]
(请注意,第137列引用了test.project.compensation.model.ClaimantPerson)
我敢肯定这与演员表有关。当我在
ClaimantPerson
下具有属性Claimant
时,如何引用CompensationCase
?以下是类/实体定义的摘要:
CompensationCase是我正在查询的类
@Entity
@Table(name = "project_compensation_case")
@EntityListeners({CompensationCaseNumberListener.class})
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class CompensationCase extends AutoIdBasedEntity{
@Valid
@NotNull(message = "{compensationCase.claimant.NotNull}")
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false)
@ForeignKey(name = "FK_CompensationCase_Claimant")
@JoinColumn(name = "claimant_id", unique = true, updatable = false, nullable = false)
private Claimant claimant;
//So on....
}
索赔人:
@Entity
@Table(name = "project_compensation_claimant")
@Inheritance(strategy = InheritanceType.JOINED)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "objectType")
@JsonSubTypes({
@JsonSubTypes.Type(ClaimantPerson.class),
@JsonSubTypes.Type(ClaimantOrganization.class)
})
public abstract class Claimant extends AutoIdBasedEntity{
//stuff here
}
这是ClaimantPerson:
@Entity
@Table(name = "project_compensation_claimant_person")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "objectType")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ClaimantPerson extends Claimant {
//properties and stuff
}
感谢任何帮助。谢谢!
最佳答案
原来我不需要.as()
方法。通过执行子查询,我可以进行伪广播。
因此,基本上我的规范的最后一行变为:
return cb.in(claimantPath).value(claimantPersonSubquery);
这个问题的答案有助于引导我正确的方法:Polymorphic JPA query with CriteriaQuery API