ARUNKUMAR ARJUNAN
ARUNKUMAR ARJUNAN

Reputation: 51

How to build predicate for a List in spring queryDSL?

    Service.java

    private String sid;
    private String serviceName;
    private List<Feature> feature;

    Feature.java

    private Long id;
    private String featureName;

I build predicates like below

     import com.querydsl.core.BooleanBuilder;
     import com.querydsl.core.types.Predicate;

     BooleanBuilder booleanBuilder = new BooleanBuilder();
     booleanBuilder.and(QService.service.id.eq(id));
     booleanBuilder.and(QFeature.feature.usoc.eq(featureUsoc));

     Predicate predicate = booleanBuilder;

     serviceRepository.findAll(predicate);

When I query the repo, I get below error

nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: illegal attempt to dereference collection [{synthetic-alias}{non-qualified-property-ref}feature] with element property reference [featureName] [select service
from com.ws.app.model.Service service
where service.id = ?2 and feature.featureName = ?3]]

How to build predicate query for a variable in entity if it is of type List?

Upvotes: 0

Views: 6156

Answers (1)

You would need a JOIN or a subquery to the correlated path:

query().from(QService.service)
    .where(QService.service.id.eq(id))
    .where(query().from(QService.service.feature, QFeature.feature)
          .where(QFeature.feature.usoc.eq(featureUsoc))
          .exists())
    .fetch()

Join:

query().from(QService.service)
    .innerJoin(QService.service.feature, QFeature.feature)
    .on(QFeature.feature.usoc.eq(featureUsoc))
    .where(QService.service.id.eq(id))
    .fetch()

Upvotes: 1

Related Questions