Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 362804 - firstResult in EJBQueryImpl cannot be reset to 0
Summary: firstResult in EJBQueryImpl cannot be reset to 0
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: David Minsky CLA
QA Contact: Project Inbox CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-03 11:02 EDT by Patric Rufflar CLA
Modified: 2022-06-09 10:07 EDT (History)
1 user (show)

See Also:


Attachments
Fix and testcase (3.59 KB, patch)
2011-11-09 16:12 EST, David Minsky CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Patric Rufflar CLA 2011-11-03 11:02:50 EDT
Build Identifier: 2.1.3

The firstResult property of an EJBQueryImpl cannot be reset to 0 once it has been previously set to a value > 0.

Because setFirstResult(0) is internally represented by -1,
the condition "if (firstResultIndex > -1)" does not hold hence it will not
update the firstResult attribute of the database query:

    protected void propagateResultProperties() {
        DatabaseQuery databaseQuery = getDatabaseQueryInternal();
....
            if (firstResultIndex > -1) {
                cloneSharedQuery();
                readQuery = (ReadQuery) getDatabaseQueryInternal();
                readQuery.setFirstResult(firstResultIndex);
            }
        }
    }


Reproducible: Always

Steps to Reproduce:
1. Create a JPQL Query using EntityManager.createQuery()
2. call setFirstResult(5)
3. execute the query using getResultList().
4. call setFirstResult(0)
5. execute the query again using getResultList()

=> firstResult of 5 is still used
Comment 1 David Minsky CLA 2011-11-09 15:45:12 EST
In EJBQueryImpl setFirstResultInternal(int startPosition), the code resets the internal firstResultIndex parameter to UNDEFINED (-1) if the startPosition is 0:

    if (startPosition == 0) {
        firstResultIndex = UNDEFINED;
    } else {
        firstResultIndex = startPosition;
    }

This has the effect of not (re)-cloning the shared query and setting the first result on the clone; So the existing shared query with the previous firstResult value is executed.

Solution is to simplify the code above to:

    firstResultIndex = startPosition;
Comment 2 David Minsky CLA 2011-11-09 16:12:02 EST
Created attachment 206738 [details]
Fix and testcase
Comment 3 David Minsky CLA 2011-11-09 17:53:28 EST
Checked into trunk (2.4) at revision: 10361
Comment 4 Patric Rufflar CLA 2011-11-09 18:48:38 EST
Thanks for fixing this so quickly.

Is there a chance of a backport to 2.3.2?
Comment 5 David Minsky CLA 2011-11-10 11:42:34 EST
Checked into /branches/2.3/ at revision: 10362
Comment 6 Patric Rufflar CLA 2011-11-10 11:46:38 EST
Thanks, David.
Comment 7 Eclipse Webmaster CLA 2022-06-09 10:07:54 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink