Pos Graduation : MIT Software Engineering – Java (Infnet College) Developer Experience: 10+ years on systems developments 7+ years as Delphi developer 3+ years as Java developer 8+ months as Java teacher Agile, Scrum, TDD SQLServer, Oracle, Postgres, MySQL Java Certifications: SCJP (1.6) SCWCD (1.5) OCBCD In Progress Hebert is a DZone MVB and is not an employee of DZone and has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

EasyCriteria – An Easy Way to use JPA Criteria

08.06.2012
| 3622 views |
  • submit to reddit
Today we will see about this tool that make easier to use the JPA Criteria. The application that uses this library will be cleaner, easier to use and portable across the JPA implementations.

At the end of this post you will find the source code to download.

What is Criteria? Currently is the best solution to create dynamic queries. Imagine a page that allows the user to do several types of queries; the requested query could be by name, by age or with both. Take a look bellow in how the query would look like if we concatenate a String:

EntityManager em = emf.createEntityManager();
String hql = "select p from Person p where 1=1 ";
 
if(parameters[0].equals("name")){
    hql += " and p.name = '" + values[0] + "'";
}
 
if(parameters[1].equals("age")){
    hql += " and p.age = " + values[1];
}
 
TypedQuery<Person> query = em.createQuery(hql, Person.class);
 
System.out.println(query.getResultList());

Notice that in the code above a String concatenation is made; remember that this practice is a bad and dangerous practice because it allows “SQL Injection” hacker attack. To avoid this attack we should use a query with parameters:

EntityManager em = emf.createEntityManager();
String hql = "select p from Person p where 1=1 ";
 
if(parameters.contains("name")){
    hql += " and p.name = :name";
}
 
if(parameters.contains("age")){
    hql += " and p.age = :age";
}
 
TypedQuery<Person> query = em.createQuery(hql, Person.class);
 
if(parameters.contains("name")){
    query.setParameter("name", values[0].toString());
}
 
if(parameters.contains("age")){
    query.setParameter("age", Integer.valueOf(values[1].toString()));
}
 
System.out.println(query.getResultList());

Notice that the SQL Injection problem were solved but now the code must check parameters to add it to the query and later to pass its values; the code needs of two “parameters searches” to complete the task.

The Java/Oracle developers had the brilliant idea when they created the Criteria concept that is perfect to this kind of situation. Check bellow how the code would look like with the native JPA Criteria:

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root);
 
if(parameters.contains("name")){
    Path<String> name = root.get("name");
    cq.where(cb.and(cb.equal(name, values[0])));
}
 
if(parameters.contains("age")){
    Path<Integer> name = root.get("age");
    cq.where(cb.and(cb.equal(name, Integer.valueOf(values[1].toString()))));
}
 
TypedQuery<Person> query = em.createQuery(cq);
 
System.out.println(query.getResultList());

Is possible to see that to pass the parameters values is easier. There is no need to concatenate the String or to check the parameters list values to populate the values.

Unfortunately the Criteria API is to complex and verbose to the extreme. If you want to do only a “select p from Person p” you would need to create the criteria below:

EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);
cq.select(root);
 
TypedQuery<Person> query = em.createQuery(cq);
System.out.println(query.getResultList());

Is to much code to do something so easy, list all persons from a table.

To avoid all this verbosity the Open Source project named EasyCriteria were created. If a developer uses the EasyCriteria the query above would look like below:

EntityManager em = emf.createEntityManager();
EasyCriteria<Person> easyCriteria = EasyCriteriaFactory.createQueryCriteria(em, Person.class);
 
if(parameters.contains("name")){
    easyCriteria.whereEquals("name", values[0]);
}
 
if(parameters.contains("age")){
    easyCriteria.whereEquals("age", values[1]);
}
 
System.out.println(easyCriteria.getResultList());

Notice that the all JPA verbosity is gone. Now it is possible to have a clean code, easier to create dynamic queries. About the code above is worth to talk about:

  • Line 2: An instance of the EasyCriteria is created through a “factory”. This factory exists to do the abstraction of the every needed steep to create an object of the EasyCriteriaImp type. In the future verions new types of the EasyCriteria will be added, e.g. “Tuple”.
  • Lines 5 and 9: It is easier to pass the parameters. To pass the parameters to compare values (“name = :name”) just use the equals method that take as first parameter the attribute name; the second parameter will be the value that will be equaled.
  • Line 12: To run the query it will not be necessary to use the Query interface. The EasyCriteria itself takes this responsibility. It is possible to extract the query result through the EasyCriteria. There are two methods available to get the query result: EasyCriteria.getSingleResult(), EasyCriteria.getResultList().

In the EasyCriteria web page it is available several code samples and the methods that can be used. Other advantage of the EasyCriteria is the ability to “link” all methods:

easyCriteria.whereEquals("name", values[0]).whereEquals("age", values[1]).getResultList();

It is an light weight library because the only dependency is the JPA that the system will need to have. Attention: your application will need to have a JPA implementation up and running.

This library was developed with JUnit and tested with Hibernate, OpenJPA and EclipseLink. The JUnit also uses the Cobertura framework to check if all code lines (or most of it) are covered by the tests, so far we got 100% of coverage.

EasyCriteria still in Beta but the development team already got planed some releases and functionalities.

Other EasyCriteria advantage is that your software code in no long “coupled” to any kind of JPA implementation. Today the Hibernate has a good criteria tool, but your code must stay “attached” to it. With the EasyCriteria you will be able to use any kind of JPA implementation. The proof of this decoupled library is that the EasyCriteria has been tested with 3 implementations quoted earlier.

The EasyCriteria has the methods: in, like, empty and others. The developer will be able to do join (just simple joins without parameter), distinct or even order by all with Criteria.

Here you will find the EasyCriteria to download and have access to all its documentation.

Click here to download the source code of this post.

I hope that this post/tool may help you.

If you have any doubt/question/comment just post it.

 

 

 

 

 

 

 

 

Published at DZone with permission of Hebert Coelho De Oliveira, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Andy Jefferson replied on Tue, 2012/08/07 - 1:51am

How is this different to something from less than 2 weeks ago?

http://java.dzone.com/articles/easycriteria-%E2%80%93-easy-way-use 

The comments on that item, particularly the ones around QueryDSL still stand IMHO

Hebert Coelho replied on Tue, 2012/08/07 - 7:21pm in response to: Andy Jefferson

Hello Jefferson, I know that there is a new version of the EasyCriteria but I can not tell about this new post. It was not me. [=

The answer about QueryDSL still the same.

I will quote it bellow:

Hebert Coelho D... replied on Thu, 2012/07/26 - 12:18pm in response to: n.roberts

Hello Nathanaël,

 The idea of EasyCriteria is to make easier to run the queries. The user will not need to create any kind of metamodels. The developer will not need to create any additional class to run the EasyCriteria. 

 The created query will result in a TypedQuery so the query will not have a type cast problem.

 As future improvements EasyCriteria will support  MetaModel. [= 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.