I am new to JPA, JPQL, Hibernate, Querydsl and now I saw that in my project is used Querydsl for querying and I am wondering why this approach has been chosen. So you can be for sure why exactly is in my project but you can tell why and in what situations Querydsl is preferred. Why we can use simple JPQL statements?
Asked
Active
Viewed 3,597 times
2 Answers
12
- You can use code completion in IDE
- IDE refactoring tools work (more-or-less)
- type-safe
- (almost)syntax-safe
- compiler generates errors on compile time of wrong types etc vs Hibernate complains just on first run
- Consistency, you can use same principles to query JPA, MongoDB, Collections...

ikettu
- 1,203
- 12
- 17
-
1If you rename your entity class property will the IDE rename field in the queryDSL query? Can user do parametric queries with QueryDSL? – Xelian Jan 15 '15 at 13:47
-
1Query defining DSL classes have to be generated from Entity definitions. And yes you can do parametric queries. – ikettu Jan 15 '15 at 13:50
-
1OK, Q Classes are generated on the base of the Entity, but Entity properties are used int the QeryDSL queries and if you change the Entity proeprties, QClass will be changes ,but what about property which you have used in your old DSL query ? It will be changed for example if you rename property field in the Entity class? Does IDE handle this change? – Xelian Jan 16 '15 at 08:43
-
1If not changed automatically but at least it cleanly breaks the build and you can find breaking places quickly. You can find instructions how to setup project on Eclipse from http://blog.mysema.com/2010/10/using-querydsl-in-eclipse.html – ikettu Jan 16 '15 at 10:07
6
I would like to make a counterpoint to the QueryDSL usage.
I don't like to use QueryDSL for the follow reasons:
- You need to compile the project frequently to maintain the
Q
classes updated - You need to setup the IDE to consider the
Q
classes generated ontarget
as normal classes. - I have already seen the IDE "get lost" and no longer recognize the
Q
classes - I already create simple QueryDSL statements that generated really weird and not predictable SQLs.
Even being friendly for final users, I prefer to use JPQL because it's closer to the generated SQL and more predictable.
But compared to Criteria, I prefer QueryDSL.

Dherik
- 17,757
- 11
- 115
- 164
-
-
All negatives stated above are caused by a lack of knowledge on how to correctly set up projects to avoid these mistakes. I downvoted your answer because it is not related if QuesryDSL is OK or NOT. – Chiffie Aug 28 '19 at 17:54
-
2Actually there is a valid argument in the first point for starter. I was searching and seems using QueryDSL from starting project with creating/changing of JPA entities it would be a damn headache to generate the code and keep track of the files frequently. – Anddo Jul 14 '20 at 23:00