Led & Sustained by

G2one Logo

Developed with

Intellij

Powered by

Spring

GORM - CRUD

CRUD Operations

Grails domain classes use dynamic persistent methods to facilitate CRUD (Create/Read/Update/Delete) operations on persistent classes:

Create

To create entries in the database, domain class instances support a "save" method which cascades to the instance relationships. In the example below we only call "save" on the author and both the Author and Book instances are persisted:

def a = new Author(name:"Stephen King")
def b = new Book(title:"The Shining",author:a)
a.books.add(b)

// persist
a.save()










In this case we're having to create both sides of the relationship manually. GORM will manage the persistence of your object model to the database, but won't manage the relationships for you.

However since 0.3 Grails provides more intelligent relationship management that will manage bidrectional relationship state through dynamic methods:

def a = new Author(name:"Stephen King")
                  .addBook( new Book(title:"The Shining") )
                  .addBook( new Book(title:"The Stand") )










This will add several books to the Author plus make sure the author property on each Book is set thus managing this burden for you.

Read

Grails supports a number of ways of retrieving domain class instances, for more detail on querying see the section on  Domain Class Querying, however to retrieve an instance if the "id" is known you can use the "get" static method:

Book.get(1)










Or to find all books the "findAll", "list" or "listOrderByX" static methods can be used:

Book.findAll() // retrieve all
   Book.list(max:10) // lists first 10 instances
   Book.listOrderByTitle() // lists all the instances ordered by the "title" property










Update

The semantics of updating differ from that of saving a domain class. It is possible to update without explicitly calling "save" with the changes automatically being persisted if no exceptions occur:

def b = Book.get(1)
b.releaseDate = new Date()










This behaviour is not always desired however paricularily when combined with validation constraints (ie you don't want your domain object saved if it is not validated). Therefore if you explicity call "save" and the object is not valid changes will not be persisted:

def b = Book.get(1)
b.title = null // can't have a null title
b.save() // won't save as fails to validate










It is sometimes handy though to make changes to your domain model programmatically if validation fails. In this way the semantics of the "validate" method differ as it won't discard the changes if validation fails:

def b = Book.get(1)
b.publisher = "Print It"

if(!b.validate()) {
   b.publisher = Publisher.DEFAULT
}










In the above case changes will still be persisted even though validation failed during the "validate" method. If you want the "validate" method to not persist changes if it fails to validate you can pass a boolean "true" argument:

b.validate(true)










Alternatively, if you want to explicitly control if changes are persisted you can use the "discard" method that when called will discard any changes:

def b = Book.get(1)
b.title = "A New Title"

// something happenedd to change your mind
b.discard()










Delete

Domain class instances can be removed from the database by using the "delete" instance method:

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










</