Try performing some basic CRUD (Create/Read/Update/Delete) operations.
Create
To create a domain class use the Groovy new operator, set its properties and call
save:
def p = new Person(name:"Fred", age:40, lastVisit:new Date())
p.save()
The
save method will persist your class to the database using the underlying Hibernate ORM layer.
Read
Grails transparently adds an implicit
id
property to your domain class which you can use for retrieval:
def p = Person.get(1)
assert 1 == p.id
This uses the
get method that expects a database identifier to read the
Person
object back from the db.
You can also load an object in a read-only state by using the
read method:
In this case the underlying Hibernate engine will not do any dirty checking and the object will not be persisted. Note that
if you explicitly call the
save method then the object is placed back into a read-write state.
In addition, you can also load an proxy for an instance by using the
load method:
This incurs no database access until a method other than getId() is called. Hibernate then initializes the proxied instance, or
throws an exception if no record is found for the specified id.
Update
To update an instance, set some properties and then simply 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()