JPA Query Language

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In previous articles, we have learned about JPA entitymanager operations. Now we know how to persist and find JPA entities individually, but we may need to find data in bulk, entities satisfying some condition etc. JPA specification provides a way to retrieve data using JPA Query Language.

JPA Query Language:

JPA Query Language provides these features

  • Aggregate functions like GROUPBY and HAVING clause just like SQL.
  • Support for inner and outer joins.
  • Conditional expressions with subqueries
  • Update and delete queries for bulk data

JPA Query Language is termed as JPQL.

Getting started with JPA Query Language.

A simple example of a JPA query will be as follows.

  • This query doesn’t SELECT data from any database table.
  • It is used to SELECT employee instances. It will return 0 or more employee instances which can be identified by alias e.
  • The alias can be used to navigate across Employee fields.

for e.g.

As we can see, alias e is used to navigate to firstName field of Employee entity. The query returns only the first name of employee.

Executing Queries:

We have seen simple example of JPA Query Language but how can we execute them to fetch results?

This will return list of all Employee entity instances.

Using JPA Query Language, all operations similar to SQL can be performed on entities, we will see them in upcoming articles.

I hope this short overview was helpful to introduce JPA Query Language

 

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

One thought on “JPA Query Language

Leave a Reply

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