Domain Classes and GORM Basics

Site Admin · 11 Sep 2026 · 7 views

Domain Classes

Domain classes in Grails represent your data model. Each class maps to a database table, and each property maps to a column. Grails uses GORM (Grails Object Relational Mapping) to handle persistence automatically.

class Book {
    String title
    String author
    Date datePublished
    static constraints = {
        title blank: false, size: 1..200
        author blank: false
    }
}

GORM Persistence

GORM provides CRUD operations out of the box. You do not need to write SQL or define repository interfaces:

// Create
def book = new Book(title: "Grails in Action", author: "Smith")
book.save()

// Read
def found = Book.get(1)

// Update
found.title = "Grails in Action 2nd Ed."
found.save()

// Delete
found.delete()

Dynamic Finders

GORM offers powerful dynamic finders that let you query data without writing HQL or SQL:

def books = Book.findByAuthor("Smith")
def recent = Book.findAllByDatePublishedAfter(someDate)

Relationships

GORM supports all standard relationship types:

  • hasMany/belongsTo - One-to-many relationships
  • hasOne - One-to-one relationships
  • hasMany with joinTable - Many-to-many relationships

Key Points

  • Domain classes map directly to database tables via GORM.
  • GORM provides automatic CRUD operations.
  • Dynamic finders simplify querying without writing SQL.
  • Constraints define validation rules and database column properties.
  • GORM supports one-to-one, one-to-many, and many-to-many relationships.
Share this post:

Comments (0)

Please login or register to comment.