HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Efficient way of handling multiple sorting and filtering using JPA

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingsortingjpaefficientwayusingmultipleandfiltering

Problem

I'm using JPA 2.0, Mojarra 2.1.9 and its component library Primefaces 3.5. I have a table in MySQL database named state_table with three columns.

  • state_id (BigInt)



  • state_name (Varchar)



  • country_id (BigInt)



state_id is a auto-generated primary key and country_id is a foreign key that references a primary key of the country table.

This table is mapped by its corresponding entity class named StateTable and the data held by this table are displayed in a Primefaces DataTable, ....

The DataTable column header contains a clickable sort area, ` for each column with a sort direction for sorting, when this area is clicked, a String, either ASCENDING or DESCENDING representing the sort order is rendered and a text box for filtering (searching) in which a user enters a search item for each column.

So ultimately, what I get in JSF managed bean is a List of type
java.util.List representing sort orders of the columns of the DataTable that a user wishes.

And a Map of type
java.util.Map representing the search column names as keys and search items of the corresponding columns as values (a search item is entered by a user in a text box on the column header of each column of DataTable).

In short, I use
List for sorting and Map for filtering/searching.

My code in one of the DAOs to get a list of rows after sorting and filtering is as follows.

``
@Override
@SuppressWarnings("unchecked")
public List getList(int first, int pageSize, List multiSortMeta, Mapfilters)
{
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(StateTable.class);
Metamodel metamodel=entityManager.getMetamodel();
EntityType entityType = metamodel.entity(StateTable.class);
Rootroot=criteriaQuery.from(entityType);
Join join = null;

//Sorting

List orders=new ArrayList();

if(multiSortMeta!=null&&!multiSortMeta.isEmpty())
{
for(SortMeta sortMeta:mu

Solution

I believe, you should not mix your architecture layers.
SortMeta is a GUI class. If you want to change your GUI-Framework, or use another one, you have to refactor your Finder. Therefore you should not use SortMeta (GUI-Class) in your business layer.

Context

StackExchange Code Review Q#27659, answer score: 3

Revisions (0)

No revisions yet.