Skip to content
New issue

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

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

Already on GitHub? # to your account

Bad SQL génération with Criteria class and complex operand #1938

Open
guirak opened this issue Nov 13, 2024 · 1 comment
Open

Bad SQL génération with Criteria class and complex operand #1938

guirak opened this issue Nov 13, 2024 · 1 comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged

Comments

@guirak
Copy link

guirak commented Nov 13, 2024

Hello,

I am having some trouble when generating queries with the Criteria class (org.springframework.data.relational.core.query)

It's seems not possible to make a query like : (a OR b) AND c => This will be generated as a OR b AND c : it's not the same result.

I have defined the following method to generate an OR's combination :

    private <T> Criteria createCriteria(
            final Class<T> entityClass,
            final BDFilterOr filter) {

        // Critères de chaque opérande

        return filter.getChildren()
                .stream()
                .map(child -> createCriteria(entityClass, child))
                .reduce(Criteria::or)
                .orElseThrow(() -> new MmiBadRequestException("Pas d'opérande généré pour le filtre OR"));
    }
    

And for the AND combination :

    private <T> Criteria createCriteria(
            final Class<T> entityClass,
            final BDFilterAnd filter) {

        // Critères de chaque opérande

        return filter.getChildren()
                .stream()
                .map(child -> createCriteria(entityClass, child))
                .reduce(Criteria::and)
                .orElseThrow(() -> new MmiBadRequestException("Pas d'opérande généré pour le filtre AND"));
    }

Doing this, I'm expecting to have an OR combination inside a AND combination rounded with parenthesis in the generated query. It's not the case, when the OR combination is the first operand of the AND combination.

To solve the problem, I have to ensure the OR sub criteria is not placed at the first position of a AND criteria : it can't be used in a generic engine...

Is this API still maintained ?
Is there any workaround to permit the use of this API in any OR / AND combination ?

Thank you.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 13, 2024
@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Nov 14, 2024
@mp911de
Copy link
Member

mp911de commented Nov 14, 2024

The API intentionally follows a style that makes it seem like you would write some criteria to not interfere with AND and OR precedence. You can create groups of nested criteria. Have you seen our QueryMapperUnitTests that show various usages and combinations of Criteria?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 14, 2024
@mp911de mp911de added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Nov 14, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants