Hibernate: A DTO projection avoids loading whole entities

Select the three columns you need instead of hydrating an entity graph you throw away.

Code
record OrderSummary(Long id, String reference, BigDecimal total) { }

@Query("""
       select new com.example.OrderSummary(o.id, o.reference, o.total)
       from Order o where o.status = :status
       """)
List<OrderSummary> summaries(@Param("status") Status status);
Output
select o.id, o.reference, o.total from orders o where o.status=?

No persistence context entries, no dirty checking, no lazy proxies.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11