EasyCriteria – An easy way to use the JPA Criteria

6

Posted on July 25, 2012 by

Share it now!

Hello, how are you?

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 bellow:

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 bellow:

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.

See you soon! \o_

Response to EasyCriteria – An easy way to use the JPA Criteria

  1. Rashmi

    Cool stuff. Liked the way EasyCriteria made code very readable.
    Does it also handle joins(inner, outer) & views with equal ease?

    • uaihebert Post author

      Hello Rashmi,

      Yes, it does! =D

      All methods that you find in the guide: http://easycriteria.uaihebert.com/?page_id=84 are available to the join.

      You can access like: easyCriteria.innerJoin("dogs");
      easyCriteria.whereJoinStringIn("dogs", "name", names); // Or NotIn
      easyCriteria.whereJoinStringLike("dogs", "name", "%y"); // Or NotLike
      easyCriteria.whereJoinSetIsEmpty("dogs", "bones");
      easyCriteria.whereJoinDateBetween("dogs", "dateOfBirth", startDate, finishDate);

      Thanks for passing by! =D

  2. Mehdi Heidarzadeh

    Thanks a lot, Good post.
    I don’t know why JPA expert group designed such a verbose API ?!!!!
    That’s why projects like querydsl has been created to make developers life easier. but EasyCriteria is really better because it is on top of JPA.
    I wish all these efforts join each other to make one fluent API instead of different projects.

    Again thanks,
    keep writing such great stuff.

    • uaihebert Post author

      Hello Medhi,

      Thanks for your support.

      Why not join forces? [=

      I know that EasyCriteria has a lot to grow and we will keep doing it.

      Thanks for passing by! =D

    • uaihebert Post author

      Hello Nicolas,

      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. [=

      The problem that I saw with QueryDSL you would need a MetaModel generated. With EasyCriteria you can use it without the need of the MetaModel classes.

      Thanks for passing by.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *