Skip to content
Advertisement

How to get Page as result in Querydsl query with fetch or fetchResults properly?

Hi what i trying to achieve here is, i want to submit Pageable data into QueryDsl query and get the result as Page, how can i do it properly? here is what i do until now :

here is my controller :

JavaScript

in my controller i submit StockViewByProductCodesDto which is looked like this :

JavaScript

and here is my service :

JavaScript

as you can see, i extract pageable information with PageUtils.pageableUtils(request), here is my pageableUtils function looked like :

JavaScript

after i got the Pageable data, i submit it into my repository, which is looked like this :

JavaScript

there is no error in my editor when viewing this my repository and i able to run my project, but when i execute my repository function, i got this error :

“org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found ‘,’ near line 1, column 38 [select count(distinct stock.productId, stock.productCode, stock.warehouseId, stock.warehouseCode, stock.branchCode, stock.branchId)nfrom com.bit.microservices.b2b.warehouse.entity.Stock stocknwhere stock.warehouseCode = ?1 and stock.productCode in ?2 and stock.branchCode = ?3 and stock.branchId = ?4]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found ‘,’ near line 1, column 38 [select count(distinct stock.productId, stock.productCode, stock.warehouseId, stock.warehouseCode, stock.branchCode, stock.branchId)nfrom com.bit.microservices.b2b.warehouse.entity.Stock stocknwhere stock.warehouseCode = ?1 and stock.productCode in ?2 and stock.branchCode = ?3 and stock.branchId = ?4]”

the problem is here, on this line :

JavaScript

when i execute that line, it give me that error, i try to get the fetchResult, because i want to get the .getTotal() for the total.

but if i execute the query with .fetch(), it worked fine, like this :

JavaScript

i got my sql result execute correctly, what did i missed here? how do i get Page result correctly?

Advertisement

Answer

Your problem could be related with an open QueryDSL issue. The documented issue has to do with the use of fetchCount but I think very likely could be also your case.

Consider the following comment in the mentioned issue:

fetchCount() uses a COUNT function, which is an aggregate function. Your query already has aggregate functions. You cant aggregate aggregate functions, unless a subquery is used (which is not available in JPA). Therefore this use case cannot be supported.

The issue also provides a temporary solution.

Basically, the idea is be able to perform the COUNT by creating a statement over the initial select. AFAIK it is not possible with QueryDsl and this is why in the indicated workarounds they access the underline mechanisms provided by Hibernate.

Perhaps, another thing that you can try to avoid the limitation is to create a database view for your query, the corresponding QueryDsl objects over it, and use these objects to perform the actual computation. I am aware that it is not an ideal solution, but it will bypass this current QueryDsl limitation.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement