我正在尝试将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&amp; BA持有它..
如何在此查询中访问子类的属性?我必须同时包括AB&amp;符号谓词的BA。
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
您需要使用JPA的2.0 Epression#as()
:
Path bCasted = (Path)b.as(AB.class);
Predicate newCastedPredicate = bCasted.get("symbol").in(input.getSymbol());
但我认为您需要为每个所需的子类单独和递归地执行上述操作。
无关:请注意in
谓词接受Collection或Expression。看看上面链接的页面中的javadoc。