开发者

Hibernate limit result inquiry

How does maxresult property of hibernat开发者_运维问答e query works? in the example below :

Query query = session.createQuery("from MyTable");
query.setMaxResults(10);

Does this get all rows from database, but only 10 of them are displayed? or this is same as limit in sql.


It's the same as LIMIT, but it is database-independent. For example MS SQL Server does not have LIMIT, so hibernate takes care of translating this. For MySQL it appends LIMIT 10 to the query.

So, always use query.setMaxResults(..) and query.setFirstResult(..) instead of native sql clauses.


SetMaxResults retrieves all the rows and displays the number of rows that are set. In my case I didn't manage to find a method that can really retrieve only a limit number of rows. My recommendation is to create the query yourself and set there "rownum" or "limit".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜