Home > database >  can't make tomahawk dataScroller work with hibernate pages
can't make tomahawk dataScroller work with hibernate pages

Time:12-20

I have a jsf page with a dataTable which has a dataScroller footer. The actual db table is huge and the table works when I get all the rows. But it is very slow due to the size. The backend uses java and hibernate and I can do paging there. Is there a way to tell dataScroller the size of the result set but only load the records for that particular page when the user clicks one of the navigation buttons? The code is not particularly relevant but I'll post it if it helps.

CodePudding user response:

Here is my solution in case anyone needs it.

  1. I needed to tell the bean which page needs loading so I added this to my t:dataScroller

    actionListener="#{mrBean.scrollEvent}"
    
  2. In my bean I implemented the method for it like this

    private int page = 1;
    ...
    public void scrollEvent(ActionEvent event) {
        if (event instanceof ScrollerActionEvent) {
            ScrollerActionEvent sae = (ScrollerActionEvent)event;
            int index = sae.getPageIndex();
            if (index > 0) {
                page = index;
            } else if (sae.getScrollerfacet().equals("next")) {
                page = page == getMaxPage() ? page : page   1;
            } else if (sae.getScrollerfacet().equals("fastf")) {
                page = page   5 > getMaxPage() ? getMaxPage() : page   5;
            } else if (sae.getScrollerfacet().equals("last")) {
                page = getMaxPage();
            } else if (sae.getScrollerfacet().equals("previous")) {
                page = page > 1 ? page - 1 : 1;
            } else if (sae.getScrollerfacet().equals("fastr")) {
                page = page > 5 ? page - 5 : 1;
            } else if (sae.getScrollerfacet().equals("first")) {
                page = 1;
            }
        }
    }
    
    private int getMaxPage() {
        return (int) Math.ceil(getCount() / (tableRows   0.0));
    }
    
    public long getCount() {
        //implement a dao method to get the count for the resultset
    }
    
  3. Get the page from hibernate based on documentation from Baeldung. Then fill up the item array with bogus objects before and after the page (the datascroller can only work with the full array of items). These bogus objects don't matter since they are not visible to the user. Only the page that the user sees contains the real objects from the db. To make it more memory efficient, one can fill the array with the same object (reusing the same pointer).

  • Related