Document Version 1.0
Copyright © 2012-2013 firstname.lastname@example.org
JPA 2.0 introduced Criteria API for persistence entity manipulation. For those who has been working with JPQL, the question he might ask himself is: what does JPA Criteria has that JPQL NOT?
In the following I will try to compare JPQL and Criteria in the following 3 aspects:
- Coding efforts
1. The persistence entity "User.java":
The "User.java" class has only 2 fields:
- id, of type Long
- name, of type String
2. Coding Effort: "SELECT" User
"SELECT" using JPQL:
"SELECT" using Criteria API:
Apparently the coding effort using JPQL is less than that when using Criteria.
3. Usability: "SELECT" User
For someone who is familar with SQL, he might feel very comfortable with JPQL syntax, however Criteria is not so straightforward.
For someone who use Criteria, he must also know what is SQL "from", "where".., so he is not going to have big problem with JPQL.
According to JPA documentation, "JPQL query is parsed for each call", where Criteria NOT, so criteria should perfoms better.
We will take data retrieve as example, to see if and how much is the performance diference actually.
Test run 1:
Test run 2:
The averave of 2 test runs: