Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class based Projections - projection class doesn't optimizes the query execution #1821

Open
alesky78 opened this issue Jun 19, 2024 · 1 comment
Labels
status: blocked An issue that's blocked on an external project change type: enhancement A general enhancement

Comments

@alesky78
Copy link

alesky78 commented Jun 19, 2024

i have an aggregate that internally has two 1 to M relationship

@Getter
@Setter
@AllArgsConstructor
@Builder
@EqualsAndHashCode(of = {"id"})
@Table("TOOL")
public class AgrTool {

    private @Id Long id;

    private @Version Long version;

    private String status;

    //my flat parameters...
    private LocalDate creationDate;
    private LocalDate acquireDate;
    private String serialNo;
    ................
    ................

     //my two  1-to-M 
    private List<VoDefinitionCalibrationInternal> calibrationInternalDefinitions = new ArrayList<>();

    private List<VoDefinitionCalibrationExternal> calibrationExternalDefinitions = new ArrayList<>();

}

based on our business logic we need to inquery just the TOOL table returing a list of object.
Pratically we don't want to fetch the two relationship (calibrationInternalDefinitions and calibrationExternalDefinitions) we need to show a table wiht the miminal set of attribute of the TOOL table

as defined in the documentation we apply the projections expecting, that the query is limited to the field exposed by the consrtuctor of the Projection Class:
(official documentation) --> If the store optimizes the query execution by limiting the fields to be loaded, the fields to be loaded are determined from the parameter names of the constructor that is exposed.

then we implemented the minimal value object without the two relationships

  • calibrationInternalDefinitions
  • calibrationExternalDefinitions
@Getter
@Setter
@AllArgsConstructor
@Builder
public class PrjTool{

    private Long id;

    private String status;

    //my flat parameters...
    private LocalDate creationDate;
    private LocalDate acquireDate;
    private String serialNo;
    ................
    ................

}

and in our repository we add two methods:

public interface RepositoryTool extends ListCrudRepository<AgrTool,Long> {

    <T> Page<T> findAllByCustomerAndEnvironment(Long customer, Long environment, Pageable pageable , Class<T> type);

    <T> Collection<T> findAllByCustomerAndEnvironment(Long customer, Long environment, Class<T> type);

analisyng the result of our Unit test where we call this two methods:

        Page<PrjTool> result = null;
        result = repository.findAllByCustomerAndEnvironment(-160L, -160L, pageRequest, PrjTool.class);
        result.forEach(p -> log.info("element:" + p));
        assertThat(result.getNumberOfElements()).isEqualTo(2);

        Collection<PrjTool> result = null;
        result = repository.findAllByCustomerAndEnvironment(-160L, -160L, PrjTool.class);
        result.forEach(p -> log.info("element:" + p));
        assertThat(result.isEmpty()).isEqualTo(false);

activating the trace of the query from the log we see that the query generate continue to do select in the relasionsip also if no define in the projection object,

o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL query
.....
.....
o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [SELECT "TOOL"."ID" AS "ID", "TOOL"."AREA" AS "AREA"... 
.....
.....
o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [SELECT "DEFINITION_CALIBRATION_INTERNAL"."ID" AS... 
.....
......
o.s.jdbc.core.JdbcTemplate               : Executing prepared SQL statement [SELECT "DEFINITION_CALIBRATION_EXTERNAL"."ID" AS... 

Expectation
only the table TOOL table should be analyzed
if required I can create a test to show the behavior

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 19, 2024
@mp911de
Copy link
Member

mp911de commented Jun 19, 2024

Related to #1803

@mp911de mp911de added status: blocked An issue that's blocked on an external project change type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: blocked An issue that's blocked on an external project change type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants