My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria API?
+2
A:
I'm pretty sure this has already been covered here on SO but I couldn't find the existing question. So, here is my point of view on the question:
- I find JPQL queries easier to write/read.
- I find the Criteria API nice for building dynamic queries.
Which is basically what you'll find in Hibernate: Criteria vs. HQL.
But there is one major difference between the JPA 2.0 Criteria API and the Hibernate's Criteria API that is worth mentioning: the JPA 2.0 Criteria API is a typesafe API and thus gives compile time checks, code completion, better refactoring support, etc. However, I don't find that the benefits outweighs the ease of use of JPQL.
To sum up, I would favor JPQL, except for dynamic queries (e.g. for multi criteria search features).
Related questions
- Hibernate: Criteria vs. HQL
- What are some of the real world example where JPA2 Criteria API is more preferable?
More resources
Pascal Thivent
2010-10-04 20:17:53
@Pascal Thank you and congrats for the 100k milestone reached :)
Vincenzo
2010-10-05 06:21:54
@Vincenzo: Thanks! Next one now :)
Pascal Thivent
2010-10-05 06:23:30