有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JPA规范中的java转换

我有以下规格:

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列是指测试、项目、赔偿、模型、索赔人)

我相信这和演员阵容有关。当我在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....
}

这是索赔人:

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

谢谢你的帮助。谢谢


共 (1) 个答案

  1. # 1 楼答案

    原来我不需要.as()方法。通过执行子查询,我可以执行伪强制转换

    因此,基本上我的规范的最后一行是:

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

    这个问题的答案帮助我找到了正确的方向:Polymorphic JPA query with CriteriaQuery API