JPA - 使用'复合值查询'在where子句中

时间:2016-04-22 18:36:17

标签: jpa eclipselink criteria-api

我希望在JPA 2.1中实现以下(对于此示例:MySQL)语句,并使用Eclipselink作为实现:

select *
from account
where (mailing_zip_code, id) > (56237, 275)
order by mailing_zip_code asc, id asc 
limit 10;

我想要实现的是在不使用查询中的偏移量的情况下实现无限滚动的搜索方法。重点关注where子句,我无法为此构造找到正确的名称。在某些地方,它被称为复合值',但我无法通过此名称找到结果,但它似乎符合SQL-92标准。

此语句将通过过滤器参数进行扩展,因此我自然希望使用JPA条件API构建它。

我现在搜索了API已经有一段时间了,现在是否有人可以这样做?

1 个答案:

答案 0 :(得分:0)

意识到这一点后

select *
from account
where (mailing_zip_code, id) > (56237, 275)
order by mailing_zip_code asc, id asc 
limit 10;

只是一种较短的写作方式

select *
from account
where mailing_zip_code > '56237'
or (mailing_zip_code = '56237' AND id > 276)
order by mailing_zip_code asc, id asc 
limit 10;

标准查询毕竟不是那么难(只附加谓词):

if (null != startId) {
     Predicate dateGreater = cb.greaterThan(entity.get(sortBy), startValue);

     Predicate dateEqual = cb.equal(entity.get(sortBy), startValue);
     Predicate idGreater = cb.greaterThan(entity.get("id"), startId);
     Predicate dateEqualAndIdGreater = cb.and(dateEqual, idGreater);

     cb.or(dateGreater, dateEqualAndIdGreater);
}

如果有更好的方式,我会很高兴了解它。

相关问题