JPQL Introduction

The JPQL (Java Persistence Query Language) is an object-oriented query language which is used to perform database operations on persistent entities. Instead of database table, JPQL uses entity object model to operate the SQL queries. Here, the role of JPA is to transform JPQL into SQL. Thus, it provides an easy platform for developers to handle SQL tasks.

JPQL is an extension of Entity JavaBeans Query Language (EJBQL), adding the following important features to it: -

  • It can perform join operations.
  • It can update and delete data in a bulk.
  • It can perform aggregate function with sorting and grouping clauses.
  • Single and multiple value result types.

JPQL Features

  • It is a platform-independent query language.
  • It is simple and robust.
  • It can be used with any type of database such as MySQL, Oracle.
  • JPQL queries can be declared statically into metadata or can also be dynamically built in code.

Creating Queries in JPQL

JPQL provides two methods that can be used to access database records. 

These methods are: -

1. Query createQuery(String name) - The createQuery() method of EntityManager interface is used to create an instance of Query interface for executing JPQL statement.

Query query = em.createQuery("Select s.s_name from StudentEntity s");  

This method creates dynamic queries that can be defined within business logic.


2. Query createNamedQuery(String name) - The createNamedQuery() method of EntityManager interface is used to create an instance of Query interface for executing named queries.

@NamedQuery(name = "find name" , query = "Select s from StudentEntity s")  

This method is used to create static queries that can be defined in entity class.


Now, we can control the execution of query by the following Query interface methods: -

  • int executeUpdate() - This method executes the update and delete operation.
  • int getFirstResult() - This method returns the first positioned result the query object was set to retrieve.
  • int getMaxResults() - This method returns the maximum number of results the query object was set to retrieve.
  • java.util.List getResultList() - This method returns the list of results as an untyped list.
  • Query setFirstResult(int startPosition) - This method assigns the position of first result to retrieve.
  • Query setMaxResults(int maxResult) - This method assigns the maximum numbers of result to retrieve.