API / Apache Grails Interview questions
What is the difference between a dynamic finder and a criteria query?
Both retrieve data through GORM without hand-written SQL, but they differ in how flexible and composable the resulting query can be, and in how the query is actually expressed in code.
| Dynamic finder | Criteria query |
| Query encoded in a method name string, e.g. findByTitleAndAuthor | Query built programmatically via a closure/builder syntax |
| Best for simple, fixed-shape lookups | Best for conditional, composable, or dynamically-assembled queries |
| Hard to build conditionally at runtime | Naturally supports adding/omitting conditions based on runtime logic |
| Limited support for joins, projections, ordering beyond simple suffixes | Full support for joins, projections, grouping, and complex ordering |
A search form where the user might supply a title, an author, both, or neither is the classic case where criteria queries win: the query can be built up condition by condition depending on which fields were actually filled in, something a single fixed dynamic-finder method name simply can't express.
More Related questions...