Announcement Announcement Module
Collapse
No announcement yet.
Page returns false for isFirstPage even if getTotalElements() is of 0 value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Page returns false for isFirstPage even if getTotalElements() is of 0 value

    Hello,

    I noticed that the org.springframework.data.domain.Page returns false for isFirstPage and hasPreviousPage returns true even if getTotalElements() is of 0 value.

    I reckon it should look like:

    2013-07-02 11:15:33,612 DEBUG [AdminController] ==========>>> Page number: 2
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Page size: 10
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Total elements: 0
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Total pages: 0
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Is first page: true
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Is last page: true
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Has next page: false
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Has previous page: false

    and NOT like:

    2013-07-02 11:15:33,612 DEBUG [AdminController] ==========>>> Page number: 2
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Page size: 10
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Total elements: 0
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Total pages: 0
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Is first page: false
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Is last page: true
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Has next page: false
    2013-07-02 11:15:33,613 DEBUG [AdminController] ==========>>> Has previous page: true

    What do you think ? Or maybe Oliver Gierke knows better than me :-)

    Kind Regards,

  • #2
    Since your code sets the page to 2 (it seems odd that you could be on page 2 and yet have no rows in your table!), PageImpl thinks it's on page 2. hasPreviousPage() determines that "getCount() > 0" and returns true.

    The test for isFirstPage() is just a negation of hasPreviousPage(). So, maybe it's too simplistic, but I'd suggest that it's weird that you have page > 0 yet there are no elements/rows in your result set.

    Just to show how org.springframework.data.domain.PageImpl behaves:

    Code:
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.PageRequest;
    
    public class PageImplOdditiesTest {
    
        private static final Logger log = LoggerFactory.getLogger(PageImplOdditiesTest.class);
    
       @Test
        public void testProblemWithPageImpl() {
            log.debug("testProblemWithPageImpl");
            List<String> accounts = new ArrayList<>();
            Page<String> p;
            int pageSize = 20;
            int totalElements = 0;
            // Paraphrasing the JavaDocs for PageRequest: "Pages are zero indexed; 0 will return the first page."
            for (int i = 0; i < 3; i++) {
                p = new PageImpl<String>(accounts, new PageRequest(i, pageSize), totalElements);
                log.debug("pageNumber: {} isFirstPage: {}; hasPreviousPage: {}; totalElements: {}",
                    new Object[] { p.getNumber(), p.isFirstPage(), p.hasPreviousPage(), p.getTotalElements() });
            }
        }
    }
    Output:
    Code:
    pageNumber: 0 isFirstPage: true; hasPreviousPage: false; totalElements: 0
    pageNumber: 1 isFirstPage: false; hasPreviousPage: true; totalElements: 0
    pageNumber: 2 isFirstPage: false; hasPreviousPage: true; totalElements: 0

    Comment


    • #3
      Hi,

      I understand your suspicion about the page 2 potentially being the cause of this. However, this page 2 is just noise and has no side effect in it.

      Indeed the number of elements is 0 because the database table was totally empty.

      This is how I found out about the issue. Simply by accident.

      Comment


      • #4
        I don't think that I was saying that page 2 with no rows is the "cause of this". Rather, I think I was saying that PageImpl was written with the (naive?) assumption that it wouldn't be fed page > 0 and 0 total elements. That assumption probably needs to be corrected because "in the wild" it seems easy to violate.

        Perhaps you should file a bug against PageImpl. If you think it's helpful, you can include my test case.

        Comment


        • #5
          Sorry if I misread you.

          I created a Jira at https://jira.springsource.org/browse/DATAJPA-366

          Comment


          • #6
            That's alright. I'm never sure that I phrase things correctly, no matter how many times I rewrite these messages (and I do a lot of rewriting in an attempt to be more clear -- never totally successful, it seems).

            Comment


            • #7
              No worries :-)

              Comment

              Working...
              X