Basic Tutorial
Queries
Projection in Spring Boot JPA allows you to fetch specific columns from a table instead of the entire entity. This improves performance by reducing data transfer.
The simplest way to fetch specific columns is by defining an interface projection
id
and name
from User
✅ Advantage:Faster than fetching the entire entity.
If you need custom queries, you can use JPQL.
✅ Advantage: More flexible, can handle complex queries.
id
, name
, and age
into a DTO✅ Advantage: Works well with custom DTO transformations.
If you need raw SQL, use @Query(nativeQuery = true)
.
✅ Advantage: Ideal for complex SQL queries with joins, group by, etc.