Logic Mismatch Between AbstractQueryImpl and LimitHelper For setMaxResults(0)
Description
Attachments
Activity

Piotr FindeisenJanuary 1, 2015 at 12:53 AM
,
currently the fix versions include 4.2.9... I'm using 4.2.15 and experience "setMaxResults(0)"-queries to return multiple results (instead of 0).
I'm constructing the Query directly from hql (should it matter).
Am I doing something wrong?

Brett MeyerSeptember 30, 2014 at 5:54 PM
, that's correct, it should be fixed from 4.3.2 and onward. If you're seeing otherwise, feel free to open a new JIRA. However, make sure to include a test case that can be used to reproduce what you're seeing.

Sebastian RicaldoniSeptember 30, 2014 at 5:10 PM
Sorry for my newbie question but I'm not aware of the release / development lifecycle of the hibernate team.
The status says 'FIXED', does this mean the updates are already published, for instance in mvnrepository.com?
The reason I'm asking is because I'm using version 4.3.6.Final and when calling setMaxResults(0) the generated SQL does not include the LIMIT 0 at the end (in Mysql) which was the original requirement in this issue.
Thanks in advance!

Brett MeyerFebruary 20, 2014 at 10:35 PM
Thanks for the pull request!
Furkan KAMACIFebruary 20, 2014 at 12:14 PM
I've created a pull request at Github.
When I set setMaxResults to 0 I expected that it works as like LIMIT 0 of SQL query syntax. However it returns all records. I've checked the code and I saw that at org.hibernate.internal.AbstractQueryImpl:
public Query setMaxResults(int maxResults) {
if ( maxResults < 0 ) {
// treat negatives specically as meaning no limit...
selection.setMaxRows( null );
}
else {
selection.setMaxRows( maxResults);
}
return this;
}
It says that will set selection's maxRows property with 0 because of maxResults is greater or equal to 0 (maxRows is 0).
However there is a line of code at org.hibernate.dialect.pagination.LimitHelper
public static boolean hasMaxRows(RowSelection selection) {
return selection != null && selection.getMaxRows() != null && selection.getMaxRows() > 0;
}
so it checks selection.getMaxRows() > 0 and does not check selection.getMaxRows() >= 0 This is a mismatch with previous class. If I change it to selection.getMaxRows() >= 0 it will work as like LIMIT 0 of SQL query syntax but I think that this is not expected. So I've changed the comparison criteria of AbstractQueryImpl and fixed comment.