The course is part of this learning path

Start course

This course covers the Java Persistence API, or JPA, which is designed to persist data between Java objects and relational databases.

Learning Objectives

  • Learn the fundamentals of the Java Persistence API
  • Learn about entities, entity inheritance, and JPA queries

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.


Basic knowledge of Java programming.


Hello dear friends. In this video, we will examine queries in JPA. Let's begin. The Java persistence API provides the following methods for querying entities; the Java persistence query language, JPQL, is a simple string-based language similar to SQL used to query entities in their relationships. The Criteria API is used to create type safe queries using Java programming language APIs to query for entities and their relationships. Both JPQL and the Criteria API have advantages and disadvantages. Just a few lines long, JPQL queries are typically more concise and more readable than Criteria queries. Developers familiar with SQL will find it easy to learn the syntax of JPQL. JPQL named queries can be defined in the entity class using a Java programming language annotation or in the applications deployment descriptor. However, JPQL queries are not type safe and require a cast when retrieving the query results from the entity manager. This means that typecasting errors may not be caught at compile time. JPQL queries don't support open-ended parameters.

Criteria queries allow you to define the query in the business tier of the application. Although this is also possible using JPQL dynamic queries, criteria queries provide better performance because JPQL dynamic queries must be parsed each time they are called. Criteria queries are type safe and therefore don't require casting as JPQL queries do. The Criteria API is just another Java programming language API and doesn't require developers to learn the syntax of another query language.  Criteria Criteria queries are typically more verbose than JPQL queries and require the developer to create several objects and perform operations on those objects before submitting the query to the entity manager. Let's look at them one-by-one. First, JPQL. As you can see in this code, first we need factory and manager again. There is a new class named query and we will create it from entity manager with create query. This one is without parameters. After that, you can use get result lists method of query to send items to any list.

You can easily add any parameter by doing something like this. Look, we can select the Charles dickens' employee with parameters. Now, let's examine the criteria. Let's look at the code snip. In criteria, you need a criteria builder. Now you can use create query method of builder. As you can see, there are no parameters. If you need parameters, you can use where or any other abstract query command. I don't want to use all of the commands because this lesson is about JPA and commands are a topic of SQL. This lesson concludes our discussion on JPA. In the next lesson, we'll start looking at security in the Java EE platform. Have a great day.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics