9
我試圖更改以下HQL使用JPA標準:CriteriaBuilder.and&CriteriaBuilder.or,how-to?
select distinct d from Department d
left join fetch d.children c
where d.parent is null
and (
d.name like :term
or c.name like :term
)
order by d.name
Department
有孩子的Set<Department>
。
標準:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Department> c = cb.createQuery(Department.class);
Root<Department> root = c.from(Department.class);
root.fetch("children", JoinType.LEFT);
Path<Department> children = root.join("children", JoinType.LEFT);
c.orderBy(cb.asc(root.get("name")));
c.distinct(true);
c.where(cb.isNull(root.get("parent")));
String param = "%" + "term" + "%";
cb.and(cb.like(root.<String> get("name"), param));
cb.or(cb.like(children.<String> get("name"), param));
TypedQuery<Department> tq = getEntityManager().createQuery(c);
departments = tq.getResultList();
我知道這可能是一個有點簡潔,然而,HQL返回24和標準版本28.我想我不會處理:
and (x = y OR z = y)
正確。任何指針將不勝感激。 謝謝。
萬分感謝。 – kmansoor
非常感謝,因爲我也一直在尋找這個。 – user75ponic