19 Querying JPA with Querydsl


This chapter covers

  • Introducing Querydsl
  • Creating a Querydsl application
  • Querying a database with Querydsl

Querying a database is essential to retrieve information that meets certain criteria. A Domain Specific Language (DSL) is a language dedicated to a particular application domain. For example, querying a database is an example of such a particular domain. This chapter focuses on Querydsl, one of the alternatives to query a database using Java programs. We’ll examine its most important capabilities and will apply them in a Java persistence project. For comprehensive documentation of Querydsl, you may access http://querydsl.com/.

19.1 Introducing Querydsl

There are various alternatives to query a database from within Java programs. You may use SQL, as it was possible since the early days of JDBC. The drawbacks of using this approach were the lack of portability (the queries are dependent on the database and the particular SQL dialect) and the lack of type safety and static query verification.

JPQL (Jakarta Persistence Query Language) was a step forward, being an object-oriented query language that is independent of the database. This means no more lack of portability but still a lack of type safety and static query verification.

19.2 Creating a Querydsl application

19.2.1 Configuring the Querydsl application

19.2.2 Creating the entities

19.2.3 Creating the test data to query

19.3 Querying a database with Querydsl

19.3.1 Filtering data

19.3.2 Ordering data

19.3.3 Grouping data and working with aggregates

19.3.4 Working with subqueries and joins

19.3.5 Updating the entities

19.3.6 Deleting entities

19.4 Summary