谁能帮助我获取下面提到的JPA查询的JPA标准查询。
SELECT p,l FROM Person p LEFT JOIN Language l ON (p.language = l.language and l.locale like :locale) AND p.name like :name AND p.time BETWEEN :startDate AND :endDate order by name asc
假设Person与语言有关,这就是您在较早的Hibernate中所做的事情:
Criteria criteria = entityManager.createCriteria(Person.class); Criteria languageCriteria = criteria.createCriteria("language"); languageCriteria.add(Restrictions.like("locale", locale)); criteria.add(Restrictions.like("name", name)); criteria.add(Restrictions.between("time", startDate, endDate)); criteria.addOrder(Order.asc("name"));
我第一次尝试使用JPA 2.0:
CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Person> criteria = builder.createCriteria(Person.class); Root<Person> pRoot = criteria.from(Person.class); Join<Person, Language> langJoin = criteria.join("language", JoinType.LEFT); Predicate conjunction = builder.conjunction(); criteria.where(builder.and( builder.like(langJoin.get(Language_.locale), locale), builder.like(pRoot.get(Person_.name), name), builder.between(pRoot.get(Person_.time), startDate, endDate)); criteria.orderBy(builder.asc(pRoot.get(Person_.name)));
请让我知道这是否适合您。
编辑: 更新查询以仅使用一个where呼叫。
where