Jpa JPQL
Jpa JPQL
This chapter tells you about JPQL and how it works with persistence units. In this chapter, examples
follow the same package hierarchy, which we used in the previous chapter as follows:
JPQL can retrieve information or data using SELECT clause, can do bulk updates using UPDATE
clause and DELETE clause. EntityManager.createQuery API will support for querying language.
Query Structure
JPQL syntax is very similar to the syntax of SQL. Having SQL like syntax is an advantage because
SQL is a simple structured query language and many developers are using it in applications. SQL
works directly against relational database tables, records and fields, whereas JPQL works with Java
classes and instances.
For example, a JPQL query can retrieve an entity object rather than field result set from database,
as with SQL. The JPQL query structure as follows.
Follow the same example employee management used in previous chapters. Here we will go
through the service classes using scalar and aggregate functions of JPQL.
package com.tutorialspoint.eclipselink.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
//Scalar function
Query query = entitymanager.
createQuery("Select UPPER(e.ename) from Employee e");
List<String> list = query.getResultList();
for(String e:list) {
System.out.println("Employee NAME :"+e);
}
//Aggregate function
Query query1 = entitymanager.createQuery("Select MAX(e.salary) from Employee e");
Double result = (Double) query1.getSingleResult();
System.out.println("Max Employee Salary :" + result);
}
}
After compilation and execution of the above program you will get output in the console panel of
Eclipse IDE as follows:
package com.tutorialspoint.eclipselink.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.tutorialspoint.eclipselink.entity.Employee;
//Between
Query query = entitymanager.createQuery( "Select e " + "from Employee e " +
"where e.salary " + "Between 30000 and 40000" );
List<Employee> list=(List<Employee>)query.getResultList( );
//Like
Query query1 = entitymanager.createQuery("Select e " + "from Employee e " +
"where e.ename LIKE 'M%'");
List<Employee> list1=(List<Employee>)query1.getResultList( );
After compilation and execution of the above program you will get output in the console panel of
Eclipse IDE as follows:
Ordering
To Order the records in JPQL we use ORDER BY clause. The usage of this clause is same as the use
in SQL, but it deals with entities. Follow the Order by example.
package com.tutorialspoint.eclipselink.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.tutorialspoint.eclipselink.entity.Employee;
//Between
Query query = entitymanager.createQuery( "Select e " + "from Employee e " +
"ORDER BY e.ename ASC" );
After compilation and execution of the above program you will get output in the console panel of
Eclipse IDE as follows:
Named Queries
A @NamedQuery annotation is defined as a query with a predefined unchangeable query string.
Instead of dynamic queries, usage of named queries may improve code organization by
separating the JPQL query strings from POJO. It also passes the query parameters rather than
embedding literals dynamically into the query string and results in more efficient queries.
First of all, add @NamedQuery annotation to the Employee entity class named Employee.java
under com.tutorialspoint.eclipselink.entity package as follows:
package com.tutorialspoint.eclipselink.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table
@NamedQuery(query = "Select e from Employee e where e.eid = :id", name = "find
employee by id")
public Employee( ) {
super();
}
@Override
public String toString() {
return "Employee [e;
}
}
package com.tutorialspoint.eclipselink.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.tutorialspoint.eclipselink.entity.Employee;
query.setParameter("id", 1204);
List<Employee> list = query.getResultList( );
After compilation and execution of the above program you will get output in the console panel of
Eclipse IDE as follows:
After adding all the above classes the package hierarchy is shown as follows:
There are two ways of fetching records from the database - eager fetch and lazy fetch.
Eager fetch
Fetching the whole record while finding the record using Primary Key.
Lazy fetch
It checks for the availability of notifies it with primary key if it exists. Then later if you call any of the
getter method of that entity then it fetches the whole.
But lazy fetch is possible when you try to fetch the record for the first time. That way, a copy of the
whole record is already stored in cache memory. Performance wise, lazy fetch is preferable.
Loading [MathJax]/jax/output/HTML-CSS/jax.js