![](/img/trans.png)
[英]JPA CriteriaBuilder left outer join for table with no relations?
[英]JPA CriteriaBuilder with junction table
下面的sql表达式如何使用CriteriaBuilder表示?
select * from Ref where prac_id = (select prac_id from loc l join staff_loc sl where sl.loc = l.id and sl.pracstaff_id = 123)
模型类
@Entity
public class Ref {
private Long id;
private Prac prac;
}
@Entity
public class Loc {
Long id;
@ManyToOne
Prac prac;
@ManyToMany
Set<PracStaff> pracStaff;
}
@Entity
public class Prac {
Long id;
@OneToMany
Set<Loc> locs;
}
@Entity
public class PracStaff {
Long id;
@ManyToMany
Set<Loc> locs;
}
我想要获取的是使用CriteriaBuilder具有ID为123的PracStaff的所有Ref对象。
这是我必须进行全面测试的解决方案。 运用
Expression<Collection<PracStaff>>
返回收藏是我所缺少的
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Ref> criteriaQuery = criteriaBuilder.createQuery(Ref.class);
Root<Ref> from = criteriaQuery.from(Ref.class);
criteriaQuery.select(from);
Subquery<Prac> subquery = criteriaQuery.subquery(Prac.class);
Root<Loc> fromLoc = subquery.from(Loc.class);
Expression<Collection<PracStaff>> pracStaffInLoc = fromLoc.get("pracStaff");
subquery.where(criteriaBuilder.isMember({pracStaffObj}, pracStaffInLoc));
subquery.select(fromLoc.<Prac>get("prac"));
Path<Prac> specialist = from.get("{field in Ref class}");
Predicate p = criteriaBuilder.equal(specialist, subquery);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.