Search code examples
hibernatejpaquerydsl

Issue with JPA Sub Query


I start using QueryDsl 4 And I got this error

java.lang.UnsupportedOperationException
    at com.querydsl.jpa.JPASubQuery.iterate(JPASubQuery.java:72)
    at com.querydsl.core.support.FetchableQueryBase.fetch(FetchableQueryBase.java:46)
    at dao.SearchProjetDao.getListProjetsQueryDsl(SearchProjetDao.java:108)
    at controllers.ProjetRest.getListProjetsQueryDsl(ProjetRest.java:82)

When I try this sub query

QProjet prj = new QProjet("prj");
...
BooleanBuilder where_loc = new BooleanBuilder();

if( bean.commune != null ){

    QLocalisation loc_2 = new QLocalisation("loc_2");

    where_loc.and(prj.id.in( 
            JPAExpressions.select(loc_2.projet.id).from(loc_2)
            .where(loc_2.commune.id.eq(bean.commune))
            .fetch()
    ));

}
...

I used to use JPASubQuery in QueryDsl 3 but it do not exist anymore in version 4


Solution

  • You have placed fetch in the wrong place.

    where_loc.and(prj.id.in( 
                JPAExpressions.select(loc_2.projet.id).from(loc_2)
                .where(loc_2.commune.id.eq(bean.commune))
             //   .fetch()  <--- Remove
        ));
    

    in expects either (among other)

    • SubQueryExpression - the overload picked by modified code
    • Collection - the overload picked by buggy code. (I assume these should be constants)

    See Subquery Examples in QueryDls4 doc