GORM — Domain Modeling & Persistence

GORM provides a simple ORM API. Example domain:

package bookstore

class Book {
    String title
    String author
    Integer pages
    Date published

    static constraints = {
        title blank:false
        author nullable:true
        pages min:1
    }
}

Basic operations

// create
def b = new Book(title:'Refactoring', author:'Martin Fowler', pages:448)
b.save(flush:true)

// query
def found = Book.findByTitle('Refactoring')

// update
found.pages = 450
found.save()

// delete
found.delete()

Relationships

class Author {
  String name
  static hasMany = [books: Book]
}
class Book {
  static belongsTo = [author: Author]
}

Transactions

Use @Transactional on services or methods for transactional behavior.