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. This chapter focuses on Querydsl, one of the alternatives for querying a database from Java programs. The “dsl” part of the Querydsl name refers to domain specific languages (DSLs), which are languages dedicated to particular application domains. For example, querying a database is such a domain.

In this chapter we’ll examine the most important capabilities of Querydsl, and we’ll apply them in a Java persistence project. For comprehensive documentation of Querydsl, see its website: http://querydsl.com/.

19.1 Introducing Querydsl

There are various alternatives for querying a database from within Java programs. You can use SQL, as has been possible since the early days of JDBC. The drawbacks of this approach are 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 there’s no lack of portability, but there’s 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 entities

sitemap