DTO Via Spring Data Class-Based Projections
Description: Fetch only the needed data from the database via Spring Data Projections (DTO). In this case, via class-based projections.
Key points:
- write an class (projection) containing a constructor, getters, setters,
equals()
andhashCode()
only for the columns that should be fetched from the database - write the proper query returning a
List<projection>
- if it is applicable, limit the number of returned rows (e.g., via
LIMIT
) - in this example, we can use query builder mechanism built into Spring Data repository infrastructure
Note: Using projections is not limited to use query builder mechanism built into Spring Data repository infrastructure. We can fetch projections via JPQL or native queries as well. For example, in this application we use a JPQL.
Output example (select first 2 rows; select only "name" and "age"):