blaze-persistence
blaze-persistence copied to clipboard
AttributeFilter on Multiset FetchStrategy
Is there a way to filter by an attribute inside a collection with FetchStrategy Multiset?
Let's suppose you have an entity view CatView which has some property List<KittensView>.
You can add an EqualFilter as viewSetting.addAttributeFilter("kittens.age", 1) and it works fine.
But if you add FetchStrategy.MULTISET you'll get something like Attribute CatView_kittens_age not found on type Cat'.
Is there a workaround here?
Hi, unfortunately there is currently no "real" workaround for this yet as filters are applied as conditions to the main query, whereas this requires applying filters into the subquery. We have a related issue since the semantics of collection filters aren't fully intuitive which we want to fix at some point: https://github.com/Blazebit/blaze-persistence/issues/109
Now the question is, do you want to filter the collection i.e. omit elements that you don't care about or do you want to exclude whole object graphs where no collection element matches your criteria?
Filtering the collection works nicely by making use of the array predicate syntax i.e.
@Mapping("kittens[age > :minAge]")
List<KittensView> getKittens();
Note though that you will have to apply the parameter "by hand" and if you need this to be "optional" you'd need to add some null handling logic i.e. kittens[:minAge is null or age > :minAge].
Filtering out object graphs will require you to use a view filter instead which roughly adds a predicate like exists(select 1 from e.collection c where ...)
Hi @beikov , can you elaborate on the Filtering the collection works nicely by making use of the array predicate syntax i.e. ? I have the following example:
@Mapping(fetch = FetchStrategy.MULTISET, value = "stations[:mwFrom is null or mw >= :mwFrom]") public List<StationMinimalView> getStations(); and I set the optional parameter -> setting.addOptionalParameter("mwFrom", 0.8); I am getting an error : java.lang.NullPointerException: Cannot invoke "com.blazebit.persistence.impl.ParameterManager$ParameterImpl.isUsedInGroupBy()" because "parameter" is null" What am I missing here?
Thanks
Hey @jbakoc1, please create a new issue and attach the full stack trace as well as the query and entity view that you use.