JPA QueryDsl 子查询故障解决方案
问题场景
我开始使用QueryDsl 4
,但出现此错误 java.lang.UnsupportedOperationException
异常代码
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)
异常SQL
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()
));
}
...
解决方案
修改后SQL
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() <--- Remove
));
}
...
注意去掉这个函数
.fetch()