This page last changed on Mar 15, 2006 by [email protected].

Domain Class Dynamic Methods & Properties

Properties

errors

Description

A list of errors from the last call to the "validate" or "save" method

Example
def b = new Book(title:"The Shining")
if(!b.validate()) {
    b.errors.each {
          println it
    } 
}

constraints

Description

A list of org.codehaus.groovy.grails.validation.ConstrainedProperty instances applied against the domain class by the constraints property (see Validation)

Example
def b = new Book(title:"The Shining")
b.constraints.each {
      println it.name
      println it.maxLength
}

properties

Description

Allows access to the domain class properties as a map and perform types conversion when set allowing properties to be set from request parameters for example.

Example
def b = new Book(title:"The Shining")
b.properties = this.params

Methods

delete

Description

Deletes a domain class instance from the database

Example
def b = Book.get(1)
b.delete()

hasErrors

Description

True if the domain class instance has errors following a call to "validate" or "save"

Example
def b = new Book(title:"The Shining")
b.validate()
if(b.hasErrors()) {
    b.errors.each {
          println it
    } 
}

ident

Description

Returns the value of the identity property of the domain class regardless of the name of the identity property itself

Example
def b = new Book(title:"The Shining")
b.save()

println b.ident()

refresh

Description

Refreshes a domain classes state from the database

Example
def b = Book.get(1)
b.refresh()

save

Description

Saves a domain class instance to the database cascading updates to any child instances if required. Returns false if validation failed and the instance was not saved

Parameters
  • validate (optional) - Set to false if validation should be skipped
Example
def b = new Book(title:"The Shining")
if( !b.save() ) {
   b.errors.each {
        println it
   }
}

validate

Description

Validates a domain class against the applied constraints (see Validation)

Example
def b = new Book(title:"The Shining")
if( !b.validate() ) {
   b.errors.each {
        println it
   }
}

Static Methods

count

Description

Counts the number of instances in the database and returns the result

Parameters

None

Example
def noOfBooks = Book.count()

createCriteria

Description

Creates a grails.orm.HibernateCriteriaBuilder instance for the domain class. (see Builders)

Example
def c = Account.createCriteria()
	def results = c {
		like("holderFirstName", "Fred%")
		and {
                   between("balance", 500, 1000)
                   eq("branch", "London")
		}
		maxResults(10)
		order("holderLastName", "desc")
	}

exists

Description

Checks whether an instance exists for the specified id and returns true if it does

Parameters
  • id (required) - The id of the instance
Example
if(Account.exists(1)) {
     // do something
}

find

Description

Finds and returns the first result for the given query or null if no instance was found

Parameters
  • query (required) - Either an HQL query or a instance of the domain class for query by example
  • arguments (optional) - A list of arguments for a named HQL query
Example
Book.find("from Book as b where b.author='Dan Brown'") // Dan brown's first book
Book.find("from Book as b where b.author=?",['Dan Brown']) // with a named parameter

def b = new Book(author:"Dan Brown")
Book.find(b) // query by example

findAll

Description

Finds all of the domain class instances for the specified query

Parameters
  • query (optional) - Either an HQL query or a instance of the domain class for query by example
  • arguments (optional) - A list of arguments for a named HQL query
  • max (optional) - The maximum number of results to retrieve
Examples
Book.findAll() // everything
Book.findAll("from Book as b where b.author='Dan Brown'",10) // The first 10 books from Dan Brown
Book.findAll("from Book as b where b.author=?",['Dan Brown'],10) // with a named parameter

def b = new Book(author:"Dan Brown")
Book.findAll(b) // query by example

findBy

Description

Dynamic method that uses the properties of the domain class to allow the creation of Grails query method expressions that return the first result of the query

Examples
Book Domain Class
class Book {
   @Property Long id
   @Property Long version
   @Property String title
   @Property Date releaseDate
   @Property String author
}
Examples
def b = Book.findByTitle("The Shining")
b = Book.findByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
b = Book.findByReleaseDateBetween(firstDate, new Date())
b = Book.findByReleaseDateGreaterThanOrEqual(firstDate)
b = Book.findByTitleLike("%Hobbit%")
b = Book.findByTitleNotEqual("Harry Potter")
b = Book.findByReleaseDateIsNull()
b = Book.findByReleaseDateIsNotNull()

findAllBy

Description

Dynamic method that uses the properties of the domain class to allow the creation of Grails query method expressions that return all instances of the domain class

Examples
Book Domain Class
class Book {
   @Property Long id
   @Property Long version
   @Property String title
   @Property Date releaseDate
   @Property String author
}
Examples
def results = Book.findAllByTitle("The Shining", [max:10, sort:"title", order:"desc", offset:100] )
results = Book.findAllByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
results = Book.findAllByReleaseDateBetween(firstDate, new Date())
results = Book.findAllByReleaseDateGreaterThanOrEqual(firstDate)
results = Book.findAllByTitleLike("%Hobbit%")
results = Book.findAllByTitleNotEqual("Harry Potter")
results = Book.findAllByReleaseDateIsNull()
results = Book.findAllByReleaseDateIsNotNull()

findWhere

Description

Uses named arguments that match the property names of the domain class to produce a query that returns the first result.

Examples
Book Domain Class
class Book {
   @Property Long id
   @Property Long version
   @Property String title
   @Property Date releaseDate
   @Property String author
}
Examples
def b = Book.findWhere(title:"The Shining", author:"Stephen King")

get

Description

Retrieves an instance of the domain class for the specified id, otherwise returns null

Examples
def b = Book.get(1)

list

Description

Lists all of the instances of the domain class.

Parameters
  • max - The maximum number to list
  • offset - The offset from the first result to list from
  • order - The order to list by, either "desc" or "asc"
  • sort - The property name to sort by
Examples
def results = Book.list() // everything
   def results = Book.list(max:10) // 10 results
   def results = Book.list(max:10, offset:100) // 10 results, offset by 100
   def results = Book.list(max:10, offset:100, sort:"title", order:"desc") // 10 results, offset by 100, orderd by title in descending order

listOrderBy

Description

Lists all of the instances of the domain class ordered by the property in the method expression

Parameters
  • max - The maximum number to list
Examples
def results = Book.listOrderByAuthor() // everything
   def results = Book.listOrderByTitle(max:10) // 10 results
   def results = Book.listOrderByTitle(max:10, offset:100, order:"desc") // 10 results, offset from 100
Document generated by Confluence on Mar 29, 2006 08:46