Skip to content

FetchableFluentQeury doesn't apply projection in the database level #4056

@yassir78

Description

@yassir78

Description:

Hello everyone, and especially the Spring Boot community 👋

I'm currently facing a small issue while working with JPA Specifications. Because my entity has a large number of fields, I’d like to perform database-level projections instead of memory-based projections for performance reasons.

From my understanding, the only way to achieve this is by using the CriteriaBuilder API directly. However, before taking that route, I wanted to ask for guidance or best practices.

Questions:

Is there a recommended approach to perform projections at the database level when using JPA Specifications?

Can this be integrated cleanly with Spring Data’s specification infrastructure, or is CriteriaBuilder the only option?

Environment:

Spring Boot version: 3.4.4
JPA provider: Hibernate

Database: PostgreSQL

`

Sort sort = buildSort(searchQuery.getSortBy(), searchQuery.getSortDir());
PageRequest pageable = PageRequest.of(searchQuery.getPageIndex(), searchQuery.getPageSize(), sort);

return orderEntityRepository.findBy(
        specification,
        query -> query
                .as(OrderSearchView.class)
                .sortBy(sort)
                .page(pageable)
                .map(orderEntityMapper::toMechanicalShopSearchResponse));

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions