Type-Safe queries in Spring Data JPA

Type-safe queries approach helps to verify the correctness of the query statement at compile time.

Thành Trần
Programming Sharing
3 min readMar 29, 2021

Have you ever face a problem that you changing the entity field and then the application throw exception at runtime due to the change? If you have been facing that problem I think using type-safe queries might be a good choice.

Problem with JPQL or Native query

When writing query statements we mostly use JPQL or native query and write literally in string.

incorrect query statement in a string-based form

I purposely wrote the wrong query but we got no warning from the compiler because it has no way to detect such errors. These errors are only thrown at runtime, therefore, if the entity class or database are changed frequently it might increase the error-prone of our application.

What is type-safe queries?

Type-safe queries refer to a writing database query statement mechanism that allows developers to verify the correctness of database query statements at compile time. Normally developers write database queries in string literal style therefore compiler has no way to check if the statements are incorrect.

How does Spring Data JPA support for type-safe queries?

To apply type-safe queries in the spring application we can choose Criteria API or Querydsl. Criteria API is an alternative way to writing queries in JPA. The executing query will be generated at runtime and typesafe of course. The Criteria API is a part of the Java Persistence API specification. Besides the Criteria API, we have a popular open-source library to write the type-safe queries and easily integrated to Spring application is Querydsl.

Both Criteria API and Querydsl use Java 6 annotation processor to generate metamodel classes that describe information about persistence class(entity class) and help the library generate correct query statements. For purpose of this article, I will not cover it in detail.

To illustrate, we will be trying to write a data access method to query data from the database following the query above:

select * from customer where id = ?

JPQL or native query

Criteria API

Look at the sample, no literal string is found in the method above that means the compiler is able to detect if any change on the entity or metamodel.

To support Querydsl, spring data provide an interface named QuerydslPredicateExecutor for common situations, developers only need to make their repository extend QuerydslPredicateExecutor.

extending QuerydslPredicateExecutor for support Querydsl in Spring Data
extending QuerydslPredicateExecutor for support Querydsl in Spring Data

Back to the example, convert the query statement above to query DSL implementation is simply like the below code snippet.

Do you see the in the Criteria example and Querydsl we have two classes one is Customer_ and another one is QCustomer? These classes are called metamodel classes which will be generated by AnnotationProcessor. I will not cover those in this article by I would share with you a resource to learn more about Metamodels classes. https://developer.ibm.com/languages/java/articles/j-typesafejpa/#metamodel-of-a-persistent-domain

Conclusion

Through this article, I introduce type-safe queries and its benefits as well as how spring support making typesafe queries. I hope this article brings you the concept of typesafe queries and might consider using it for your application in its fit.

Sign up to discover human stories that deepen your understanding of the world.

Written by Thành Trần

Software Engineer, Blogger at Programming Sharing. Enjoy technology, economics, and finance.

No responses yet

What are your thoughts?