JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

JPA 2 Criteria API

December 28, 2013 by Krishna Srinivasan Leave a Comment

One of the important feature in JPA 2.0 is using the Criteria API for forming the SQL queries. If you would have worked with the JPA 1.0 version, you must be familiar with the Java Persistence Query Language (JQL) which is similar to writing the SQL queries. This approach is good for the developers who are much familiar with SQL syntax and programming, for the Java developers who are not comfortable with SQL will leads to lot of syntax error. One of the dis-advantage with JQL is, it can not capture the grammar errors at the compile time. If you have errors at SQL, it is thrown errors at the run time. Criteria API solves this problem by providing he strongly typed metamodel objects (Read : Generate MetaModel Using Eclipse or Ant) for writing the queries.

For the beginners who are not familiar with the JPA concepts, please read our article on Introduction to JPA before start reading this tutorial.

JPA Criteria API vs JPQL

JPQL queries are defined as the SQL strings. The only difference with the normal SQL is that JPQL has its own grammar for querying the datbase. For simple static queries, using JPQL is preferred. It will be easy for you to write a query instead of forming it using the CriteriaBuilder. When you build query at run time, writing plain SQL query would have unnecessary string concatenation operations which will leads to lot of syntax issues which can not be detected at the compile time.

String based JPQL queries and JPA criteria based queries are equivalent in power and performance. Choosing one method over the other is also a matter of personal choice. If you choose for your  projects, please consider the pros and cons of each method. However, JPA itself recommends using the Criteria API.

Problem with JPQL Query

Lets look at the below code:

[code lang=”java”]
EntityManager em = …;
String jpql = "select e from Employee where e.id > 20";
Query query = em.createQuery(jpql);
List result = query.getResultList();
[/code]

If you look at the above code, it has one error. The correct code is below:

[code lang=”java”]
String jpql = "select e from Employee e where e.id > 20";
[/code]

These errors are caught at the run time. That is the main dis-advantage of using the JPQL query.

A Simple JPA Criteria Query

The following query represents a simple JPQL query:

[code]
select * from employee e
[/code]

The same query can be built using the JPA criteria API as below:

[code lang=”java”]
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> e = cb.createQuery(Employee.class);
Root<Employee> ee = e.from(Employee.class);
e.select(ee);
[/code]

CriteriaBuilder is the main factory for getting the criteria queries. You can get this object by using the EntityManagerFactory or EntityManager interface.

Typesafe Criteria Query with Metamodel

I have explained in the previous post about the metamodel. It helps you to understand the metamodel and requires understanding the below code.

[code lang=”java”]
EntityManager em = …
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> c = qb.createQuery(Employee.class);
Root<Employee> p = c.from(Employee.class);
Predicate condition = qb.gt(p.get(Employee_.id), 20);
c.where(condition);
TypedQuery<Employee> q = em.createQuery(c);
List<Employee> result = q.getResultList();
[/code]

The above code has many new things which is introduced from JPA 2. But, here we have to understand the TypedQuery which is the important concept for making the query is strongly typed.  Also look at the “Employee_” which is generated as part of the metamodel.

Reference Links

  • Dynamic, typesafe queries in JPA 2.0
  • JPA Query API
  • JPA 2.0 Features

Filed Under: Java EE Tagged With: Hibernate 4 Tutorials, JPA 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved