Announcement Announcement Module
Collapse
No announcement yet.
Paging Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Paging

    Hi all,

    I am using Spring 2.0.6 and Hibernate 3.2.2 to access my database. I have a search form which gets information out of the database, the search result returns is a huge amount of data. To avoid that this has to be handelt in memory I do pagination on the database side. I output the 10 first entries and when the user clicks next it should hit the database again an retrive the next 10 entries. Is there way in Spring to make pagination easier? Any hint, code or Documentation would be helpfull.

    Thanks,

  • #2
    Spring has support for it: PagedListHolder. You can also search the forums for it.

    Joerg

    Comment


    • #3
      Hi,

      Thanks for the hint I had a look at it and also checked the Petstore sample. I think this is what I am looking for.

      Cheers;

      Comment


      • #4
        Hi,

        I am trying to solve exactly the same problem with the same version of spring/hibernate. I would greatly appreciate if you could pass me any info and/or code samples.


        thanks in advance.

        Comment


        • #5
          Have you searched the forum? There have been some threads about paging lately.

          Joerg

          Comment


          • #6
            Yes I have and got some ideas. My understanding is that Spring provides client side solution through PagedListHolder and other flavor of this. It only works if you preload all the data but if you are expacting large result set which I am, I should look at serverside solution through hibernate and let the database take care of what it does best.


            Code for hibernate is pretty simple. It is little more work from after that all the way to present it to UI. I hoping if someone has already written some code I could reuse it since I am new to both spring and hibernate. This way I would have better chance of avoiding mistakes and be able to present better solution to my manager.

            Comment


            • #7
              Originally posted by jmalkan View Post
              My understanding is that Spring provides client side solution through PagedListHolder and other flavor of this. It only works if you preload all the data but if you are expacting large result set which I am, I should look at serverside solution through hibernate and let the database take care of what it does best.
              What's client-side and server-side in your understanding? PagedListHolder is Spring and so running on the server. By implementing the PagedList stuff properly it should be possible to not preload all data. Have a look at this post about the idea using RefreshablePagedListHolder and PagedListSourceProvider. But beware, since I haven't used this paging stuff yet and I don't know if the guy in the other thread has figured out its problem it still might contain errors.

              Joerg

              Comment


              • #8
                Hi

                I have solved the problem by wrting my own Paging class using Hibernate, but I am looking to find a solution using Spring. As soon I as got a proper solution I will post it here.

                Comment


                • #9
                  By server-side I meant the database while client is the J2EE Server. I came accross some postings that suggested Hibernate is the better solution for paging through data. See this http://forum.springframework.org/sho...ination&page=2

                  Comment


                  • #10
                    Spring JDBC Pagination Tutorial

                    Here's how I implemented database level pagination using Spring JDBC:

                    http://www.codesuccess.com/tutorials/spring/pagination/

                    Thanks,

                    Andy Grove
                    Code Success
                    http://www.codesuccess.com/

                    Comment


                    • #11
                      Code for hibernate is pretty simple. It is little more work from after that all the way to present it to UI. I hoping if someone has already written some code I could reuse it since I am new to both spring and hibernate. This way I would have better chance of avoiding mistakes and be able to present better solution to my manager.
                      Hi,
                      I came across the same problem and wrote some code to enable the Spring-PageListHolder to work on a not-fully-loaded list. The problem is that PageListHolder directly works on a java.util.List. This is not necessary, though, since it only needs to know
                      1. the absolute size of list elements, and
                      2. the elements current page list.
                      Sorting is another issue, but since I only sort on the database level, I left this aside.
                      If these two informations are extracted into an interface "IPagedList", we can perfectly make the PageListHolder work with Hibernate Paging. Database-Lists just have to implement the "IPagedList"-interface and can delegate the sublist-calls to a HibernatePaging-Query.

                      Here is excerpts from my code:
                      Code:
                      /**
                       * This is almost an exact copy of the Spring-PagedListHolder (@see {@link PagedListHolder})
                       * It only differs in one single respect:
                       * The source on which the holder works is not a java.util.List (@see getSource), but
                       * an interface of the type IPagedList. By this way, the list does not have to be fully
                       * loaded - it is enough for this DynamicPagedListHolder to know the size of list elements
                       * and to be able to access the current pagelist (@see getPageList()).
                       * Thus, this DynamicPagedListHolder can be used for Paging operations, in which not the
                       * full size of elements is loaded at once, but only the page list - such as hibernate paging.
                       */
                      public class DynamicPagedListHolder implements Serializable {
                      
                      	...
                              private IPagedList source;
                              ...
                      
                      }
                      
                      
                      /**
                       * Interface for a paged list.
                       * The page list has to communicate its full size ("fullSize()") and to be able
                       * to return a sublist for given indices.
                       */
                      public interface IPagedList {
                      
                      	/**
                      	 * Returns the list's full size
                      	 * @return the list's full size
                      	 */
                      	int fullSize();
                      
                      	/**
                      	 * Returns a sublist of the list for the given indices
                      	 * @param firstIndex the first index of the request
                      	 * @param lastIndex the last index of the request 
                      	 * @return a a sublist of the list for the given indices
                      	 */
                      	List subList(int firstIndex, int lastIndex);
                      
                      	/**
                      	 * Performs a sort for the underlying list
                      	 * @param sort the sort definition to be used
                      	 */
                      	void sort(SortDefinition sort);
                      	
                      }
                      
                      /**
                       * Abstract class that can be used for a PagedList.
                       * It provides a caching mechanism for the current sublist and the list's full size
                       */
                      public abstract class AbstractQueryResultPagedList implements IQueryResultPagedList {
                      	
                      	/**
                      	 * The list's full size
                      	 */
                      	private int fullSize = -1;
                      	/**
                      	 * The list's current index
                      	 */
                      	private int currentIndex = -1;
                      	/**
                      	 * The list's current QueryResultItems
                      	 */
                      	private List<QueryResultItem> currentQueryResultItems;
                      
                      	/**
                      	 * Determines the list's full size.
                      	 * This needs to be done only once in the list's life-cycle, since we
                      	 * estimate that the list will not be changed while the user pages through it.
                      	 * 
                      	 */
                      	@Override
                      	public final int fullSize() {
                      		if (fullSize == -1){
                      			fullSize = getFullSize();
                      		} 
                      		return fullSize;
                      	}
                      
                      	/**
                      	 * Provides sorting.
                      	 * Our sorting is completely done within the database, so we do nothing here.
                      	 */
                      	@Override
                      	public final void sort(SortDefinition sort) {
                      		// nothing to do here since sorting is done within the database query
                      	}
                      
                      	/**
                      	 * Returns a sublist from the complete list with the specified indices.
                      	 * If the list for the requested index has already been loaded, we 
                      	 * return the currently loaded list.
                      	 */
                      	@Override
                      	public List subList(int firstIndex, int lastIndex) {
                      		if (currentIndex != firstIndex){
                      			currentQueryResultItems = loadList(firstIndex, lastIndex);
                      			currentIndex = firstIndex;
                      		}
                      		return currentQueryResultItems;
                      	}
                      	
                      	/**
                      	 * Returns the list's full size
                      	 * @return the list's full size
                      	 */
                      	protected abstract int getFullSize();
                      	
                      	/**
                      	 * Load the list of QueryResultItems for the specified indices.
                               * Subclasses may perform Hibernate Paging here.
                      	 * @param firstIndex the first index to be loaded
                      	 * @param lastIndex the last index to be loaded
                      	 * @return the list of QueryResultItems for the specified indices.
                      	 */
                      	protected abstract List<QueryResultItem> loadList(int firstIndex, int lastIndex);
                      
                      }
                      Cheers,
                      Heiko

                      Comment

                      Working...
                      X