我正在尝试动态地构建查询,接下来的目标是添加 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)
感谢您的时间。我将欣赏所有的评论!