JPQL vs. Criteria vs. Native Queries
With JPQL and native SQL queries, Hibernate supports 2 query languages that you can use to search data in your database. In addition to that, there is also JPA’s Criteria API that replaced Hibernate’s proprietary Criteria API.
It might seem like there are 3 different ways to do the same thing. But that’s not the case. Each type of query has specific strengths, limitations, and weaknesses, making it a great fit for some but not all use cases.
In today’s stream, we will discuss the different types of queries you can use with Hibernate and when you should prefer them.
If you want to dive deeper into the different types of queries supported by Hibernate, you should read the following articles:
- JPQL – How to Define Queries in JPA and Hibernate
- Native Queries – How to call native SQL queries with JPA & Hibernate
- Hibernate’s Query APIs
Live Online Workshops
If you enjoyed the live stream, you will love my live online workshops. Find out more at thorben-janssen.com/workshops.
Question of the Week
When you use JPQL’s path operator “.”, you can navigate a to-one association without specifying a JOIN expression in your FROM clause. But that doesn’t mean that Hibernate doesn’t need to generate such an expression when it generates the SQL statement. This often causes unexpected results:
This week’s tutorials
Do you know Hibernate’s query spaces? They are used to decide when entities need to be flushed and which parts of the cache need to get invalidate when performing a bulk update. Most of the time, Hibernate manages its query spaces automatically but there are some situations in which it needs some help.
I explained all of that in great detail in this week’s post: Hibernate Query Spaces – Optimizing Flush and Cache Operations.
One of the big strengths of the Spring ecosystem and especially of Spring Boot is the integration of popular libraries. The integrations of Flyway and Liquibase are good examples of that. Both libraries are the industry standard for version based database migrations, and Spring Boot makes using them incredibly simple.
I show you how to integrate these 2 popular libraries into your application in this week’s video: