org.hibernate.Criteria.setFetchSize()不起作用

时间:2016-02-17 09:51:23

标签: java hibernate

我有以下代码并尝试限制检索的记录数。 (LIMIT),但我总是得到所有的行。我看到论坛建议我使用Query,但是不可能使用条件来实现它吗?

Criteria criteria = session.createCriteria(Application.class)
                    .add(Restrictions.gt("lastModifiedOn", applicationLastRunTime))
                    .add(Restrictions.eq("lead", false))
                    .addOrder(Order.asc("lastModifiedOn"));
criteria.setFetchSize(40);
criteria.list()

1 个答案:

答案 0 :(得分:2)

尝试使用:

public Criteria setMaxResults(int maxResults)
Set a limit upon the number of objects to be retrieved.
Parameters:
maxResults - the maximum number of results
Returns:
this (for method chaining)

Criteria criteria = session.createCriteria(Application.class)
                    .add(Restrictions.gt("lastModifiedOn", applicationLastRunTime))
                    .add(Restrictions.eq("lead", false))
                    .addOrder(Order.asc("lastModifiedOn"));
criteria.setMaxResults(40);
criteria.list()

更多信息:What is difference between setMaxResults and setFetchSize in org.hibernate.Query?