4

我正在尝试将 JPA 的 2.0 条件查询与 Hibernate 的底层实现一起使用。我在尝试访问 where 子句中子类的属性时遇到问题。

以下是实体的简要说明:

   - Class A (which is a Entity & a Table by itself)
   - Class B (which is a Entity & a Table by itself)
   - Class A has OneToMany relationship with B
   - Class AB & BA are child classes of B (Both are entities but are not mapped to any tables. It holds few properties specific to it.)
   - Let us suppose both classes AB & BA has a property '''symbol'''

现在是条件查询:

CriteriaBuilder qb = futuresEntityManager.getCriteriaBuilder();
CriteriaQuery<A> query = qb.createQuery(A.class);
Root<A> a = query.from(A.class);
Join<A,B> b = a.joinSet("aLegs", JoinType.INNER);

现在当我形成谓词时:

predicate=qb.and(qb.equal(a.get("id").get("accountId"), "1234"));
predicate=qb.and(predicate,b.get("symbol").in(input.getSymbol()));

----- 上述行在运行时不起作用,因为 b 不包含对符号的引用。只有 AB 和 BA 的子班持有它..

如何在此查询中访问子类的属性?对于符号谓词,我必须同时包含 AB 和 BA。

有人可以帮我解决这个问题吗?

4

1 回答 1

0

您需要使用 JPA 的 2.0 Epression#as()

Path bCasted = (Path)b.as(AB.class);
Predicate newCastedPredicate = bCasted.get("symbol").in(input.getSymbol());

但我认为您需要为每个所需的子类分别递归地执行上述操作。

不相关:注意in谓词接受集合或表达式。查看上面链接页面中的 javadoc。

于 2013-08-24T02:58:10.703 回答