kenn3th
kenn3th

Reputation: 1275

Get native SQL from QueryDSL JPA (JPAQuery or JPQLQuery)

I have a requirement to count the number of group by records for pagination purpose. For example:

select count(*) from ( 
    select name, count(id) from customer
    group by name
);

However it couldn't be achieved via QueryDSL due to the limitation of JPA where JPQL doesn't allows to select count from sub query.

Is it possible to get the native SQL from QueryDSL JPAQuery or JPQLQuery? My plan is to construct and execute the select count native SQL statement via EntityManager.

String subQueryNativeSQL = "..."; // native SQL from QueryDSL
Query q = em.createNativeQuery("select count(*) from (" + subQueryNativeSQL + ")");
long count = (long) q.getSingleResult();

Upvotes: 0

Views: 2468

Answers (1)

bsiamionau
bsiamionau

Reputation: 8229

Both JPAQuery and JPQLQuery implement Projectable interface and implement count() method - and you ain't need subqueries

Upvotes: 2

Related Questions