在 JPA 规范中铸造

Casting in JPA specification

我有这个规格:

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

以下是 class/entity 定义的片段:

CompensationCase 是我正在查询的class

@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
}

这是索赔人:

@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