【发布时间】:2011-03-26 09:05:57
【问题描述】:
我正在尝试动态构造查询,我的下一个目标是添加 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)中有特定的注解
感谢您的宝贵时间。我会感谢所有类型的 cmets!
【问题讨论】:
-
您能提供带注释的域类或其他示例类吗?
标签: java api join jpa-2.0 criteria-api