How To Enrich DTO With Virtual Properties Via Spring Projections
Note: You may also like to read the recipe, "How To Create DTO Via Spring Data Projections"
Description: This is an application sample that fetches only the needed columns from the database via Spring Data Projections (DTO) and enrich the result via virtual properties.
Key points:
- we fetch from the database only the author
name
andage
- in the projection interface,
AuthorNameAge
, use the@Value
and Spring SpEL to point to a backing property from the domain model (in this case, the domain model propertyage
is exposed via the virtual propertyyears
) - in the projection interface,
AuthorNameAge
, use the@Value
and Spring SpEL to enrich the result with two virtual properties that don't have a match in the domain model (in this case,rank
andbooks
)