0

I'm writing this on the fly on my phone, so forgive the crappy code samples.

I have entities with a manytomany relationship:

@JoinTable(name="foo", @JoinColum="...",         @InverseJoinColumn="...")
@ManyToMany
List list = new ArrayList();

I want their data to be retrieved in a paginated way.

I know about setFirstResult and setMaxResults. Is there a way to use this with the mapping? As in, I retrieve the object and get the list filled with contents equal to the amount of records for a single page, with the appropriate offset.

I guess I'm just unclear of the best way to do this. I could just manually use hibernate criteria to have the effect, but I feel thats missing the API. I have this mapping, I want to see if there's a way to use it in a paginated way.

PS. If this is impractical, just say. Also, if it is, can I still use the mapping to add new entries to the join table. As in, if the entity is a persisted entity in the DB, but I haven't fetched the manytomany list, can I add something new to it and when its persisted with cascade all it'll be added to the join table without clearing the other entries?

craigmiller160
  • 5,751
  • 9
  • 41
  • 75

1 Answers1

0

The type of the relationship between entities that are part of your query isn't that important. There are a couple of ways to tackle this.

  1. If your database supports the LIMIT keyword in it's queries, you would be able to use it to get data sets, assuming you sort your data. Note that if your data changes while your user is navigating between pages, you might see some duplication or miss some records. You'll be stuck having to rewrite if your database changes to one that doesn't have the LIMIT keyword.

  2. If you need to freeze the data at the point of the original query you need to use a 3rd party framework or write your own to fetch a list of Ids for your query then split up that list and fetch by id in a subset for pagination. This is more reliable can be made to work for any database.

Displaytag is a data paging framework I've used and that I therefore can tell you works well for large datasets. It's also one of the older solutions for this problem and is not part of an extended framework. http://displaytag.sourceforge.net/11/tut_externalSortAndPage.html

Table sorter is another one I came across. This one uses JQuery and fetches the entire data set in one query, so strictly speaking it doesn't meet your "fetches the data in a paginated way" criteria. (This might not be appropriate for large sets). http://tablesorter.com/docs/

This tutorial might be helpful: http://theopentutorials.com/examples/java-ee/jsp/pagination-in-servlet-and-jsp/

If you're already using a framework take a look at whether that framework has tackled pagination: Spring MVC provides a data pager http://blog.fawnanddoug.com/2012/05/pagination-with-spring-mvc-spring-data.html

GWT provides a data pager: http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/cellview/client/SimplePager.html

The following refrences might be helpful too:

JDBC Pagination which also points to: http://java.avdiel.com/Tutorials/JDBCPaging.html

Community
  • 1
  • 1
Sammy
  • 467
  • 3
  • 13