[英]Hibernate's `setFirstResult` not working
I have a class that returns all the entities in a query, limited by a max and a min: 我有一个可返回查询中所有实体的类,受最大和最小限制:
@SuppressWarnings("unchecked")
public static <E> List<E> getAll(Class<E> klass, Integer startAt) {
startAt = (startAt == null) ? 0 : startAt;
int endAt = startAt + Constant.API_MAX_RESULTS;
return (List<E>) HibernateUtil.getCurrentSession()
.createCriteria(klass)
.setFirstResult(startAt)
.setMaxResults(endAt)
.list();
}
Where Constant.API_MAX_RESULTS
is 100
. 其中
Constant.API_MAX_RESULTS
为100
。 So for example, if I want 100-199 genes, I will call: 因此,举例来说,如果我想要100-199个基因,我会打电话给:
getAll(Gene.class, 100);
If I print startAt
and endAt
, I correctly see 100
and 200
, but what I actually get returned is 0-199 inclusive genes. 如果我打印
startAt
和endAt
,我会正确看到100
和200
,但实际上返回的是0-199个包含性基因。
When I configure Hibernate to show the SQL, I see this: 当我将Hibernate配置为显示SQL时,会看到以下内容:
select this_.id as id8_0_, ... from gene this_ limit ?, ?
Should values be set on limit
? 是否应将值设置为
limit
? Why isn't the lower bound working? 为什么下限不起作用?
Hibernate's show_sql
shows the SQL with parameter placeholders, so it does not show the actual values. Hibernate的
show_sql
显示带有参数占位符的SQL,因此它不显示实际值。 (It is logged at TRACE level though). (尽管它是在TRACE级别记录的)。
You problem is that you did not understand what setMaxResults()
does, it sets the max. 您的问题是您不了解
setMaxResults()
作用,它设置了最大值。 number of items fetched. 获取的项目数。
.setMaxResults是要返回的最大记录数,因此我相信您希望setMaxResults为100。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.