Projections
Return less than the whole entity
Sometimes a scenario needs only two fields from a large entity, such as a name and email for a recipient list.
Returning a full Customer in that case is not always convenient.
Spring Data JPA supports projections — representations of a result in a narrower shape.
interface CustomerSummary {
String getName();
String getEmail();
}
interface CustomerRepository extends JpaRepository<Customer, Long> {
List<CustomerSummary> findByActiveTrue();
}
Spring sees that the method returns the CustomerSummary interface rather than the entity
and provides an object with the specified properties.
The method contract therefore immediately shows which data the calling code needs.
A projection is the shape of a result
Projections are especially useful for read-only scenarios: lists, cards, reports, and API responses where a full entity with all its fields and relationships is unnecessary. Besides interface-based projections, Spring Data JPA can also work with DTO representations when the result is more convenient as a separate class or record.