开发者

java persistance query language limit resultset

Suppose I have following rows in a table "mytable" id

1

2

3

... ...

500

and my query is like select m from mytable m where m.id < 300 how can I get following output in the same order?

201

202

... ... ...

299

I am using setMaxResult(100) but it returns me output like this

1

2

... ...

10开发者_JAVA技巧0

any advice?


String ql = "select m from mytable m where m.id < 300 order by m.id";
Query query = em.createQuery(ql);
query.setFirstResult(200);
query.setMaxResults(100);


You can use setFirstResult to define the start index. Together with setMaxResult you can get a specific range.


If you are taking about JPA, you can use order by.

Either directly in your JPA query:

select m from mytable m where m.id < 300 order by m.id

Or using criteria:

CriteriaQuery<MyTable> q = cb.createQuery(MyTable.class);
Root<MyTable> c = q.from(MyTable.class);
q.select(c);
q.orderBy(cb.asc(c.get("id")));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜