2016-04-22 77 views
0

我想实现如下(此示例:MySQL的)“复合价值”的查询用的EclipseLink的JPA 2.1语句的执行:JPA - 在where子句

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,现在有没有人,如果这是可能的?

回答

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); 
} 

如果有更好的方法,我会很乐意了解它。

+0

如果mailing_zip_code大于'56237'且id值小于276,那么您的较长查询会给出结果。'(mailing_zip_code,id)>(x,y)'的含义是什么?我觉得很惊讶。 – carbontax

+0

你从哪里得到'不到276'的部分?我看到一个错字(276而不是275),但声明清楚地表明“id> 276”。我在Markus Winand的演示中发现了这样一句话:http://image.slidesharecdn.com/paginationdonethepostgresqlrecv2-130530141009-phpapp02/95/pagination-done-the-right-way-23-638.jpg?cb=1403756206(http ://de.slideshare.net/MarkusWinand/p2d2-pagination-done-the-postgresql-way REF = HTTP://use-the-index-luke.com/blog/2013-07/pagination-done-the -postgresql-way) – javahippie

+0

Typo放在一边,我读取查询返回所有帐户集合的联合,其中mailing_zip_code大于'56237'(其中包括任何值的id)和所有帐户的集合,其中mailing_zip_code ='56237 '和id> 276 – carbontax