我正在尝试动态构建查询,我的下一个目标是添加 JOIN 子句(我不知道如何使用 API)。
例如,到目前为止,这段代码对我有用:
...
Class baseClass;
...
CriteriaBuilder cb = JpaHandle.get().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(this.baseClass);
Root entity_ = cq.from(this.baseClass);
Predicate restrictions = null;
...
restrictions = cb.conjunction();
restrictions = cb.and(restrictions, entity_.get("id").in(this.listId));
...
cq.where(restrictions);
...
Query qry = JpaHandle.get().createQuery(cq);
(注:JpaHandle 来自 wicket-JPA 实现)
我的愿望是添加 JOIN 子句(尽可能通用)!
我在类中有特定的注释(this.baseClass)
例如 :
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "assay_id", nullable = false)
那么,在标准 JPA 中是否有类似的方法? (注意:这不能编译)
这里有一个实际的失败方法:
...
Join<Experiment,Assay> experimentAssays = entity_.join( entity_.get("assay_id") );
或者像这样:
...
CriteriaQuery<Customer> q = cb.createQuery(Customer.class);
Root<Customer> c = q.from(Customer.class);
SetJoin<Customer, PurchaseOrder> o = c.join(Customer_.orders);
对我来说,如果它可以尽可能更通用,那就太好了......:
...
Join joinClause = entity_join(entity_.get("assay_id"), entity2_.get("id"));
当然,我在类(this.baseClass)中有特定的注释
感谢您的时间。我将不胜感激各种评论!