Java JPA规范中的铸造

Java JPA规范中的铸造,java,hibernate,jpa,specifications,Java,Hibernate,Jpa,Specifications,我有以下规格: public static Specification<CompensationCase> containsClaimantName(final String firstName, final String middleName, final String surName, final String motherMaidenName) { return new Specification<CompensationCase>() {

我有以下规格:

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);
        }
    };
}
请注意,第137列是指test.project.compension.model.ClaimentPerson

我相信这和演员阵容有关。当我有一个财产索赔人在赔偿案件中时,我如何才能推荐索赔人

以下是类/实体定义的片段:

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
}
这是索赔人:

@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);
这个问题的答案帮助我找到了正确的方向:

return cb.in(claimantPath).value(claimantPersonSubquery);