Learn about JPA Query Language

Normally, when we need to manipulate any database we have to write SQL based on the information of the columns, the tables in the database, and possibly for different database types, the syntax of the SQL statement is different. To remove these drawbacks, you guys can use JPA Query Language (JPQL). How is it in details? Let’s find out in this tutorial.

First, I will create a Maven project as an example:

Learn about JPA Query Language

Since I’m going to use JPA with Hibernate’s implementation, so I’ll add Hibernate dependencies like this:

Project Lombok:

The first thing I need to tell you about JPA Query Language is that it allows us to define queries based on entities rather than the names of columns and tables in the database.

The structure and syntax of JPA Query Language are very similar to the structure and syntax of SQL statements. For example, for SELECT statements, the JPA Query Language syntax is as follows:

or with DELETE,

UPDATE

This makes it easy to define queries using the JPA Query Language, but you should remember that although we define queries using entities, in practice, at runtime, Hibernate or any library that implements JPA will transform those query statements into SQL statements for the database with the column name and table name of the database.

Let’s say, I will now use the MySQL database as an example for this tutorial. I have a table like this:

Entity Clazz as follows:

To work with MySQL database, we need to use MySQL Driver dependency:

JPA configuration file for MySQL database:

In the database, I have the following data:

Learn about JPA Query Language

Now I will try to use JPA Query Language to get information from the clazz table to try to see how it is.

First, we will get the EntityManager object:

Next, we will use the EntityManager object to get the Query object based on the Clazz entity’s information:

In the above code, we passed the createQuery() method a JPA Query Language. Do you find it like the SQL that we usually use to query the database? 😀 The only different thing is that it now uses the name of the entity of clazz table, not the name of the table clazz.

You also see that here we have given the entity Clazz an alias c just like in the SQL statement. This alias can be used as a variable in Java, meaning that if you want to get information about the properties of the Clazz entity, you can use “c.<properties>.” For example, if you want to retrieve the column name information in the clazz table, you can rewrite the JPA Query Language as follows:

Now that you have the Query object, you can use it to get the results you want:

This Query object has many methods, depending on what your JPA Query Language is, then use its method accordingly. Here, my JPA Query Language statement retrieves all records in the clazz table, so I use the getResultList() method.

After you have finished, you can print the result with the following code:

Result:

Learn about JPA Query Language

The entire code is as follows:

 

Learn about JPA Query Language
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
4Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

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