我正在尝试将 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。
有人可以帮我解决这个问题吗?