Casting required for queries with multiple entity paths

81 views
Skip to first unread message

Dmytro Maidaniuk

unread,
Aug 15, 2016, 8:23:42 AM8/15/16
to Querydsl
Hi,

I can't find in documentation any full example with usage of QueryDSL for multiple paths. For example, for next code

new JPAQuery<Void>(getEntityManager()).from(qEntityOne, qEntityTwo)
    .where(qEntityOne.fieldA.eq(qEntityTwo.fieldB))
    .leftJoin(
qEntityOne.fieldC).fetchJoin()

Java compiler said that class QueryBase don't have .leftJoin() method. I need first cast query back to JPAQuery and only after that call left join.

What I'm doing wrong?

Thanks,
Dmytro

Ruben Dijkstra

unread,
Aug 15, 2016, 9:03:52 AM8/15/16
to Querydsl
Could you check that you are not using rawtypes?

I copied your query and inserted entity types that I had, and it compiled.

However, using raw types, the compiler only inserts the upper bound, which is QueryBase.
This results in the compiler error you encountered.

Br,

Ruben

Dmytro Maidaniuk

unread,
Aug 15, 2016, 10:27:16 AM8/15/16
to Querydsl
Hi Ruben,

Nop, I don't use rawtypes here. I also used my own entity types for this query.
Casting needed after calling method where(...) which return not a JPAQuery, but QueryBase.

Could you post here your full query sample, please?

Thanks,
Dmytro

Dmytro Maidaniuk

unread,
Aug 15, 2016, 11:18:33 AM8/15/16
to Querydsl
Hi Ruben,


I found reason for casting problem. I missed Void type in return statement of helper method.

Now I can't apply Projections.map(key, value) as described in example :

Map<String, String> map = query()
                               
.select(Projections.map(employee.firstname, employee.lastname))
                               .from(employee)
                               .fetch();

Method fetch() return a list, not a map. In QueryDSL 3.6.x was a method query.map(...) for this purpose. What I need to do in QueryDSL 4.x now?

Regards,
Dmytro

Ruben Dijkstra

unread,
Aug 15, 2016, 1:58:05 PM8/15/16
to Querydsl
It seems I probably misinterpreted the question back then.

I adjusted the comment, since most people would want the GroupBy API for making Map results.

query()
.from(employee)
.transform(GroupBy.groupBy(employee.firstName).as(employee.lastName))


Then the map is keyed by the value of the expression, and not the expression itself.

I hope this clears up the confusion.

Br,
Ruben

Dmytro Maidaniuk

unread,
Aug 16, 2016, 2:59:37 AM8/16/16
to Querydsl
Hi Ruben,

Yes, correct. Now it works.
Also I found more old your answer for similar question here: https://groups.google.com/forum/#!topic/querydsl/1rMMrewx9Ts
Thanks for help with QueryDSL 4 API!

Regards,
Dmytro
Reply all
Reply to author
Forward
0 new messages