我有这个规范:
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)
我确信这与选角有关。当我在 CompensationCase
下有属性 Claimant
时,如何引用 ClaimantPerson
?
以下是类/实体定义的片段:
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....
}
这是 claim 人:
@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
}
这是 claim 人:
@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
关于java - 按照 JPA 规范进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759635/