6.1.1 Basic CRUD - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.0.11
6.1.1 Basic CRUD
Try performing some basic CRUD (Create/Read/Update/Delete) operations.Create
To create a domain class use Map constructor to set its properties and call save:def p = new Person(name: "Fred", age: 40, lastVisit: new Date()) p.save()
Read
Grails transparently adds an implicitid
property to your domain class which you can use for retrieval:def p = Person.get(1) assert 1 == p.id
Person
object back from the database.
You can also load an object in a read-only state by using the read method:def p = Person.read(1)
def p = Person.load(1)
Update
To update an instance, change some properties and then call save again:def p = Person.get(1)
p.name = "Bob"
p.save()
Delete
To delete an instance use the delete method:def p = Person.get(1) p.delete()