Blazebit / blaze-persistence

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

Problem using @ViewFilter to get List<EntityView> #1883

Open coder0112 opened 7 months ago

coder0112 commented 7 months ago

Hello!

Description

I try to get list of entity view with custom filter but I get an error

"class com.blazebit.persistence.view.impl.metamodel.FlatViewTypeImpl cannot be cast to class 
com.blazebit.persistence.view.metamodel.ViewType (com.blazebit.persistence.view.impl.metamodel.FlatViewTypeImpl and 
com.blazebit.persistence.view.metamodel.ViewType are in unnamed module of loader 'app')"

there is part of my

EntityViewUtils is below

public <T> EntityViewSettingProcessor<T> getSettingsProcessorWithFilter(Class<T> clazz, Map<String, Object> attributeFilterMap, Map<String, Object> optinalParamMap) {

EntityViewSetting<T, CriteriaBuilder<T>> setting = EntityViewSetting.create(clazz);

setting.addAttributeFilters(attributeFilterMap);
setting.addOptionalParameters(optinalParamMap);
setting.addViewFilter("customFilter");
return entityViewSetting -> setting;
}

I wrote custom filter below

@EntityView(CourtWarningEntity.class)
@ViewFilter(name = "customFilter", value = CustomFilterProvider.class)
public interface CourtProcessEntityView {

  CourtWarningLitigationStatus getLitigationStatus();

  @Mapping("count(id)")
  Long getCountByStatus();

  class CustomFilterProvider extends ViewFilterProvider {

    @Override
    public <T extends WhereBuilder<T>> T apply(T whereBuilder) {
      return whereBuilder.where("resident.company.id").eq(":companyId");
    }
  }
}

I used Spring Boot JPA. It is working with pagination.

Expected behavior

Actual behavior

Steps to reproduce

Environment

Version: 1.6.9
JPA-Provider: Hibernate 6.2.6.Final DBMS: PostgreSQL
Application Server: Java Spring Boot

beikov commented 7 months ago

Hi, can you please post the full stack trace?

coder0112 commented 7 months ago

Hi, can you please post the full stack trace?

Hi.

2024-04-02T15:07:01.090+05:00 ERROR [user-service,,] 8200 --- [nio-8400-exec-1] u.c.s.exception.GlobalExceptionHandler   : Unknown error occurred

java.lang.ClassCastException: class com.blazebit.persistence.view.impl.metamodel.FlatViewTypeImpl cannot be cast to class com.blazebit.persistence.view.metamodel.ViewType (com.blazebit.persistence.view.impl.metamodel.FlatViewTypeImpl and com.blazebit.persistence.view.metamodel.ViewType are in unnamed module of loader 'app')
    at com.blazebit.persistence.view.impl.EntityViewSettingHelper.applyViewFilters(EntityViewSettingHelper.java:298)
    at com.blazebit.persistence.view.impl.EntityViewSettingHelper.apply(EntityViewSettingHelper.java:122)
    at com.blazebit.persistence.view.impl.EntityViewManagerImpl.applySetting(EntityViewManagerImpl.java:1235)
    at com.blazebit.persistence.spring.data.base.query.AbstractPartTreeBlazePersistenceQuery$QueryPreparer.createQuery0(AbstractPartTreeBlazePersistenceQuery.java:256)
    at com.blazebit.persistence.spring.data.base.query.AbstractPartTreeBlazePersistenceQuery$QueryPreparer.createQuery(AbstractPartTreeBlazePersistenceQuery.java:226)
    at com.blazebit.persistence.spring.data.base.query.AbstractPartTreeBlazePersistenceQuery$QueryPreparer.createQuery(AbstractPartTreeBlazePersistenceQuery.java:441)
    at com.blazebit.persistence.spring.data.base.query.AbstractPartTreeBlazePersistenceQuery.doCreateQuery(AbstractPartTreeBlazePersistenceQuery.java:165)
beikov commented 7 months ago

Ok, so it seems that right now, we only support view filters on entity views that have a @IdMapping defined, though I'm not sure if there is a good reason for this limitation. Since you seem to want to get the count for every litigation status, you might as well annotate that getter i.e. @IdMapping CourtWarningLitigationStatus getLitigationStatus();

coder0112 commented 7 months ago

Thank you very much for not ignoring my problem and helping me solve the problem. It works. It was my bad