In the post today we will see about the new version of the framework named EasyCriteria. At the end of this post we will see what is to come here in the blog. Unfortunately the JPA criteria has a huge problem that is its verbosity. Why not make easier? Thinking like this that EasyCriteria framework were born and now it is on version 2.0. To do a simple JPQL with the JPA Criteria the following code would be needed:
CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class); Root<Person> root = criteriaQuery.from(Person.class); criteriaQuery.select(root); TypedQuery<Person> query = entityManager.createQuery(criteriaQuery); query.getResultList();
Notice all the code needed to do the query: select p from Person p. Check below how easy it is to do the same Criteria with EasyCriteria:
EasyCriteria<Person> easyCriteria = EasyCriteriaFactory.createQuery(entityManager, Person.class); easyCriteria.getResultList();
The difference between version 1.0 and the 2.0 version is in the reduction of methods into generic methods. To compare methods with =, >=, <, <=, and other methods just use the API like below:
easyCriteria.andGreaterThan('hairSize', 10.4f); // > easyCriteria.andGreaterOrEqualTo('height', 11.00d); // >= easyCriteria.andLessThan('totalBooksOwned', 30L); // < easyCriteria.andLessOrEqualTo('firstJobDate', firstJobDate); // <= easyCriteria.andJoinEquals('dogs', 'age', 15); // = easyCriteria.andJoinStringIn('dogs', 'name', names); // value in (x, i, z, ...) easyCriteria.andJoinStringLike('dogs', 'name', '%y');
One of the ideals of the EasyCriteria framework is to expose the lower number possible of API to the user. Thinking like this, EasyCriteria added the “OR” condition to its API without new classes. Take a look below to see how to do queries with OR:
select s from Song s where s.id = 1 or s.length = 40 or s.artist = 'Group 1 Crew'
easyCriteria.orEquals('id', 1).orEquals('length', 40).orEquals('artist', 'Group 1 Crew');
Another type of OR could be done:
select s from Song s where (s.id = 1) or (s.id = 2) or (s.length = 40) or (s.artist = 'Group 1 Crew')
easyCriteria.orEquals('id', 1, 2).orEquals('length', 40).orEquals('artist', 'Group 1 Crew');
And it is possible to do complexes queries cases like:
select s from Song s where (s.totalDownloads = 20 or s.weight = 10.00) and (s.price = 20.00 or s.type = :type)
easyCriteria.orEquals(1, 'totalDownloads', 20L).orEquals(1, 'weight', 10.00f).orEquals(2, 'price', 20.00d).orEquals(2, 'type', SongType.PRAISE);
The idea of index were added to the API to be used with a group of OR. The first OR group is composed of “totalDownloads” and “weight”, the second group has “price” and “type” as elements. As default index the value 1 is always used. The criteria above could be written as:
easyCriteria.orEquals('totalDownloads', 20L).orEquals('weight', 10.00f).orEquals(2, 'price', 20.00d).orEquals(2, 'type', SongType.PRAISE);
It is possible create a query to do the inverse of the query above, AND separated by OR:
select s from Song s where (s.id = 1 and s.name = 'Sing Out') or (s.id = 2 and s.name = 'Alive')
easyCriteria.addAndSeparatedByOr(1, 'id', 1).addAndSeparatedByOr(1, 'name', 'Sing Out').addAndSeparatedByOr(2, 'id', 2).addAndSeparatedByOr(2, 'name', 'Alive');
That is all for today, I hope you like the new version. It will take a long time to consider this framework complete. Helping with the OR functionality I could count with João Neves (http://about.me/joaonevesfilho) and Sasaki (http://curriculum.rodrigosasaki.com/). Here it is possible to see the framework official page: http://easycriteria.uaihebert.com/. And the best, it is open source (http://code.google.com/p/easy-criteria/).
Other fact about the framework is that it is 100% covered with tests using Cobertura. EasyCriteria was tested with OpenJPA, EclipseLink and Hibernate. Now it is time to ask for your help. It was found bugs in the tested implementations; it would be wonderful if you spare 5 minutes of your time voting in the bugs. With your vote the bug could be corrected faster.
- EclipseLink: https://bugs.eclipse.org/bugs/show_bug.cgi?id=386354
- OpenJPA: https://issues.apache.org/jira/browse/OPENJPA-2333
- Hibernate: https://hibernate.onjira.com/browse/HHH-7985
What is to come in the blog? It took so much time for me to write this post because I am finishing my first book. If God’s will, the book will be released in March. In this pause of new posts here, I have studied a lot of Maven and I want my next post to be about a mini book about a basic Maven and full web applications. Yes, I said “applications” in plural.
Java Platform, Enterprise Edition is a widely used platform for enterprise server programming in the Java programming language.
This book covers exciting recipes on securing, tuning and extending enterprise applications using a Java EE 6 implementation.The book starts with the essential changes in Java EE 6. Then they will dive into the implementation of some of the new features of the JPA 2.0 specification, and look at implementing auditing for relational data stores.They will then look into how they can enable security for their software system using Java EE built-in features as well as using the well-known Spring Security framework. They will then look at recipes on testing various Java EE technologies including JPA, EJB, JSF, and Web services.Next they will explore various ways to extend a Java EE environment with the use of additional dynamic languages as well as frameworks.At the end of the book, they will cover managing enterprise application deployment and configuration, and recipes that will help you debug problems and enhance the performance of your applications.