Projections and paging
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
return only the columns a screen needs, one page at a time.
Planned topics
- A projection is a read-only view: an interface with getters or a record.
- It selects only the needed columns, no entity tracking.
Pageablecarries page number, size, and sort;PageRequest.of(0, 20, Sort.by("name")).- Return
Page<T>(with total count) orSlice<T>(no count query). - Map to a response DTO in the service; never return entities from controllers.
Draft outline details
Demonstration
public record ProductListItem(Long id, String name, BigDecimal priceAmount) {}
@Query("select new com.shop.ProductListItem(p.id, p.name, p.price.amount) from Product p")
Page<ProductListItem> list(Pageable pageable);Check your understanding
What does Page have that Slice does not?
Show answer
The total count.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.