Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 328730

Summary: Query.setMaxResults(0) returns unexpected results
Product: z_Archived Reporter: lists
Component: EclipselinkAssignee: Project Inbox <eclipselink.documentation-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: acnt4reg, christopher.delahunt
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description lists CLA 2010-10-26 11:37:11 EDT
Build Identifier: EL 2.1.0

Probably easiest to point to the forum discussion:

http://www.eclipse.org/forums/index.php?t=msg&th=197236&start=0&S=d5dfec1e841c62517ccb6136e8aab980

Generally, on MySQL, if you setMaxResults() to 0, you end up losing the LIMIT portion of the query altogether, resulting in all results being returned. This is very counterintuitive. "LIMIT #, 0" works as expected in MySQL (it returns 0 results) and so it should with EL/JPA. I can understand that a negative entry for MaxResults is undefined, but 0 seems very obvious. It should be documented in the Javadocs in any case as to the behavior of negative values being passed (and 0 if you do not agree that it should behave as expected or for some reason need to make a special case due to some other requirements).

Reproducible: Always

Steps to Reproduce:
1. query.setFirstResult(1).setMaxResults(2).getResultList();
2. View the SQL generated. Should look like "...LIMIT ?, ?  bind => [1, 2]".
3. query.setFirstResult(1).setMaxResults(0).getResultList();
4. View the SQL generated. Should look like "..." with no LIMIT statement at all. Should look like "...LIMIT ?, ?  bind => [1, 0]".
Comment 1 Chris Delahunt CLA 2010-10-27 13:11:33 EDT
From the database perspective of how LIMIT works, this seems reasonable.  But from the JPA setMaxResults perspective, it must work the same regardless of the database being used.  JPA mandates that setMaxResults() throws IllegalArgumentException if the argument is negative, and is seems modeled after java.sql.Statement's setMaxRows method.  Since there is no mechanism to unset maxResults once set, 0 was chosen instead keeping it inline with the previously mentioned setMaxRows method.  

I have changed this bug to be a documentation bug since I could not find this behavior mentioned in EclipseLink docs.
Comment 2 Eclipse Webmaster CLA 2022-06-09 10:04:01 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:10:57 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink