API / Apache Grails Interview questions
Explain the internal working of a GORM dynamic finder at runtime?
Because a dynamic finder like findByTitleAndAuthor() never appears as real source code, GORM has to intercept the call at the moment it happens and figure out, from the method name string alone, what query the caller actually intended.
Since the method genuinely doesn't exist on the class, Groovy's dynamic dispatch machinery (methodMissing, part of the language's metaprogramming support) hands the call to GORM's own dynamic method handler, which strips the recognized prefix (findBy, findAllBy, countBy), splits the remaining text on conjunction keywords like And/Or, and checks each resulting segment against the domain class's actual properties — a property that doesn't exist throws a runtime exception right there, which is exactly the compile-time-blind-spot GORM Data Services were built to avoid.
Once every segment resolves to a real property (optionally with a recognized comparison suffix like GreaterThan or Like), GORM assembles the equivalent query programmatically and executes it, returning either a single instance, a list, or a count depending on which prefix the original method name started with.
More Related questions...