GORM Querying in Modern Grails
Site Admin
· 11 Sep 2026
· 9 views
Modern GORM Features
GORM in Grails 5/6/7 includes performance improvements, better query optimization, and enhanced support for both blocking and non-blocking data access.
Basic Queries
// Static methods
Book.list() // All books
Book.list(max: 10, offset: 5) // Pagination
Book.get(1) // By ID
Book.findByName("Groovy") // Dynamic finder
// Criteria queries
Book.createCriteria().list {
eq('author', 'Smith')
gt('pages', 200)
order('title', 'asc')
}
HQL Queries
// Hibernate Query Language
Book.executeQuery("from Book b where b.author = :author", [author: "Smith"])
// Projections
Book.executeQuery("select b.author, count(b) from Book b group by b.author")
Named Queries
class Book {
String title
String author
int pages
static namedQueries = {
byAuthor { String author ->
eq('author', author)
}
popularBooks {
gt('pages', 300)
}
}
}
// Usage
Book.byAuthor("Smith").list()
Book.popularBooks().list(max: 5)
Query Performance
GORM 6+ includes query caching and batch loading improvements:
static mapping = {
cache true // Enable query caching
batchSize 10 // Batch loading
}
Key Points
- GORM provides static methods, dynamic finders, and criteria queries.
- HQL offers powerful query capabilities beyond dynamic finders.
- Named queries encapsulate reusable query logic.
- Query caching and batch loading improve performance.
- GORM 6+ includes non-blocking query support.