0

我正在使用 EL,当我运行下面的查询时,我一直得到 0。我想获取当前处于活动状态的申请人 (AP) 的数量。子实体申请人是个人,我想避免查询个人的所有元素?

@RooJavaBean
@RooToString
@RooEntity(identifierColumn = "personID", inheritanceType = "SINGLE_TABLE")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("P")
public class Person {

    @NotNull
    @Size(min = 1, max = 50)
    private String FirstName;

    @NotNull
    @Size(min = 1, max = 50)
    private String LastName;
}

子实体“申请人”

@RooJavaBean
@RooToString
@RooEntity
@DiscriminatorValue("AP")
public class Applicant extends Person{

    private String major;

    private String nativeLanguage;

    private String ethnicity;

    private String hispanic;
}

我的查询尝试:

   /**
     * 
     * @return
     */
    public int getCountActiveApplicants(){

        EntityManager entityManager = factory.createEntityManager();
        int value = entityManager.createQuery("select count(distinct o) from Person o where o.TYPE = \"AP\" AND o.active = \"Yes\" ").getFirstResult();

        System.out.println("wowzer " + value + "\n");
        return value;
    }
4

1 回答 1

0

你为什么不简单地计算申请人数?

select count(distinct a) from Applicant a where a.active = true

EclipseLink 将在 SQL 中对其进行转换,并为您在鉴别器上添加 where 子句。请记住,JPQL 与您的实体及其持久字段/属性一起使用。它知道它们的关联和它们的继承层次结构。JPQL 从不使用表名和列名。

(旁注:为什么对布尔字段使用“是”?)

于 2012-05-22T21:02:56.120 回答