JPA继承查询(Eclipse Link)

时间:2016-04-13 13:38:26

标签: java postgresql jpa eclipselink

考虑以下架构:

public class A {
    @Id
    int id;
    String fieldA;
}
public class B extends A {
    String fieldB;
}
public class C extends A {
    String fieldC;
}

如何以{strong> 所有 A的方式查询C并仅获取 B的{​​{1}}?

我尝试使用处理进行向下转换,但似乎fieldB="someValue"上的过滤器正在处理所有父类(B)。

首先是否可以进行此类查询?

1 个答案:

答案 0 :(得分:2)

您可以使用'type'关键字创建一个按类型过滤的查询和一个存在的子查询来过滤C实体:

select a
from A a
where type(a) = :type
or exists (
  select 1
  from B b
  where b.id = a.id
  and b.fieldB = :fieldB
)

通过调用查询实例的setParameter方法传递实体C的类类型。