How to select count distinct for QueryDSL projection

8,482 views
Skip to first unread message

Dmitry Gusev

unread,
Feb 7, 2014, 5:23:59 AM2/7/14
to quer...@googlegroups.com
Hello,

I have declared custom QueryDSL projection like this:

    @QueryProjection

    public PEntity(Long id, Object... custom)


and I use it with query.list() like this:

ConstructorExpression<PEntity> expr = QPEntity.create(PEntity.class

                selectExprs.toArray(new Expression[selectExprs.size()]));

query

     .distinct()

     .list(expr);


Now I'd like to select count for the same query, how can I do this?


I was hoping to just replace .list(expr) with .singleResult(expr.count()) or .singleResult(expr.countDistinct()),

but ConstructorExpression doesn't have count methods.


Maybe it's possible somehow to get count for entire JPAQuery instance?

Something like "select count(*) from (JPAQuery) t;" ?

timowest

unread,
Feb 7, 2014, 3:24:42 PM2/7/14
to quer...@googlegroups.com
Hi.

ConstructorExpression defines a projection of elements, count is a different kind of projection. You can't mix them.

You can get the count

* in general form  = query.count()
* for a path or operation = query.singleResult(expr.count())
* as a wildcard count = query.singleResult(Wildcard.count)

Br,
Timo

Dmitry Gusev

unread,
Feb 8, 2014, 2:08:10 AM2/8/14
to quer...@googlegroups.com
But I was talking about count distinct.

I can't just do query.countDistinct() because I need number of unique rows among these specific columns and not just among primary keys of some entity.

What I was trying to say is that I can select distinct multiple columns using .list() and ConstructorExpression, but I can't select count distinct for this query.
And looks like none of the proposed solutions work for this case, am I right?

Timo Westkämper

unread,
Feb 8, 2014, 5:52:43 AM2/8/14
to Querydsl on behalf of Dmitry Gusev
On Sat, Feb 8, 2014 at 9:08 AM, Dmitry Gusev via Querydsl <querydsl+noreply-APn2wQevfV_6wxY...@googlegroups.com> wrote:
But I was talking about count distinct.

I can't just do query.countDistinct() because I need number of unique rows among these specific columns and not just among primary keys of some entity.

What I was trying to say is that I can select distinct multiple columns using .list() and ConstructorExpression, but I can't select count distinct for this query.
And looks like none of the proposed solutions work for this case, am I right?

Count distinct with multiple arguments is not supported in JPQL as far as I know.

Timo
 

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


Reply all
Reply to author
Forward
0 new messages