9 Steps for dynamic filtering and paging of a JPA Entity


Folks, this isn’t by any means a show of brilliant software engineering, but I didn’t find anything exactly like it in the blogosphere. I have a really basic scenario to solve.  I have a JQuery grid in a jsp with filterable columns which are mapped via Stripes MVC and Spring to a JPA Entity.

The ajax call from JQuery gives me name/value pairs to filter.  It also gives me the row number to begin the page with and how many rows to retrieve.  From this, I can filter the entity list without hard-coding the Predicates or without building a JPQL string.  The following method goes in Spring Repository or where ever you are keeping business logic for entities.

Step 1:  #44 – Get a CriteriaBuilder from the entity manager

Step 2: #47 – Get a Root of type <YourEntity>

Step 3: #49 – declare a collection of Predicates

Step 4: #60 – For each name/value pair, instantiate a new Like Predicate and add it to the list.  Use The % sign around the data for wildcard searching

Step 5: #62 – Add all predicates to the query’s where clause

Step 6: #63 – create TypedQuery from the CriteriaQuery

Step 7: #70 – set the first row for your page

Step 8: #71 – set your max your for your page

Step 9: #72 – retrieve the data

FilterPaging

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s