Blazebit / blaze-persistence

Rich Criteria API for JPA providers
https://persistence.blazebit.com
Apache License 2.0
742 stars 90 forks source link

IS NOT NULL not possible in correlationExpression #1915

Open markHervagault opened 5 months ago

markHervagault commented 5 months ago

Description

When I want use ":aOptionnalParam IS NOT NULL" in correlationExpression, I have this compil error :

An error occurred while trying to resolve the condition expression 'providersAffectToLeg.rowid = correlationKey.rowid AND (:transportationModeFilter IS NOT NULL OR transportationMode.rowid = :transportationModeFilter)' of the attribute legSolutions[fr.**.api.dao.projection.ProviderAffectToLegSummaryProjection.getLegSolutions]: Illegal occurence of [:transportationModeFilter] in path chain resolver! Parameters are not allowed as results in mapping. Please use @MappingParameter for this instead!

Steps to reproduce

This correlationExpression not working :

@EntityView(ProvidersAffectToLeg.class)
public interface ProviderAffectToLegSummaryProjection {

    @IdMapping
    Long getRowid();

    ProviderSummaryProjection getProvider();

    @MappingCorrelatedSimple(
            correlationBasis = "rowid",
            correlated = LegSolution.class,
            correlationExpression = "providersAffectToLeg.rowid IN correlationKey " +
                    "AND (:transportationModeFilter IS NULL OR transportationMode.rowid=:transportationModeFilter)",
            fetch = FetchStrategy.JOIN
    )
    List<LegSolutionSummaryProjection> getLegSolutions();
}

This correlationExpression working :

@EntityView(ProvidersAffectToLeg.class)
public interface ProviderAffectToLegSummaryProjection {

    @IdMapping
    Long getRowid();

    ProviderSummaryProjection getProvider();

    @MappingCorrelatedSimple(
            correlationBasis = "rowid",
            correlated = LegSolution.class,
            correlationExpression = "providersAffectToLeg.rowid IN correlationKey " +
                    "AND transportationMode.rowid=:transportationModeFilter",
            fetch = FetchStrategy.JOIN
    )
    List<LegSolutionSummaryProjection> getLegSolutions();
}

It's a bug ?

beikov commented 4 months ago

Looks like a bug, yes. What are you trying to achieve here? Can't you instead filter against ProvidersAffectToLeg#rowid directly?

markHervagault commented 4 months ago

I'll take a simpler example to explain my need. Here is the data model:

CatView:
    kittens: KittenView[]

KittenView:
    name: String

And here is the cat I have in my database :

garfield:
    - kittens :
        - name: Sushi
        - name: Mickey
        - name: Oliver

I want to fetch all cats that have kittens with an "e" in their name and fetch only those kittens. Here's the result I want:

garfield:
    - kittens :
        - name: Mickey
        - name: Oliver

If I use a filter on kittens, I'd get this answer:

garfield:
    - kittens :
        - name: Sushi
        - name: Mickey
        - name: Oliver

That's why I wanted to overwrite the default mapping of my collection The solution I've found is to let the default mapping on my view, and process the result of my query during my view->dto mapping to remove unwanted kittens. That works too, but I'm fetching kittens for nothing.

Thanks !

beikov commented 4 months ago

If you want to filter the collection, why not just add a @AttributeFilter(ContainsFilter.class) on KittenView#getName() and activate that via addAttributeFilter("kittens.name", "e")?