API / Apache Grails Interview questions
What is the difference between GORM criteria queries and HQL?
Both let a developer express queries more powerful than dynamic finders, but they differ in syntax style and how strongly they're checked at compile time versus expressed as free-form text.
| Criteria queries | HQL (Hibernate Query Language) |
| Built programmatically with a Groovy closure/builder DSL | Written as a query string, syntactically similar to SQL |
| Naturally composable: add/remove conditions in code | String concatenation needed to build conditional queries dynamically |
| IDE support and refactoring tend to work more naturally against Groovy code | Query text isn't checked by the compiler; errors surface at runtime |
| Good default choice for most GORM query needs | Useful for developers already comfortable with SQL-like syntax, or porting existing HQL |
Neither is strictly "better" in every case — criteria queries are generally the more idiomatic, Groovy-native choice for building queries programmatically, while HQL remains available for teams that prefer its SQL-like syntax or need to express something that maps more directly onto a query they already know how to write in that style.
More Related questions...