$ grace create-domain-class Person
4 Quick Start Guide
Version: 2023.3.0
Table of Contents
4 Quick Start Guide
A domain class can be created with the create-domain-class
command if you are using Grace, or if you are not using Grace you can just create the .groovy
file manually:
This will create a class at the location app/domain/com/example/Person.groovy
such as the one below:
package com.example
class Post {
static constraints = {
}
}
If you have the configured the dataSource.dbCreate property and set it to "update", "create" or "create-drop", GORM will automatically generate/modify the database tables for you.
|
You can customize the class by adding properties:
package com.example
class Person {
String name
Integer age
Date lastVisit
static constraints = {
}
}
Once you have a domain class try and manipulate it with console
command in Grace by typing:
$ grace console
This loads an interactive GUI where you can run Groovy commands with access to the Spring ApplicationContext, GORM, etc.
Or if you are not using Grace here is a unit test template (using Spock) that can be run to test out the examples:
import spock.lang.*
import grails.gorm.annotation.Entity
import grails.transaction.Rollback
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager
class ExampleSpec extends Specification {
@Shared @AutoCleanup HibernateDatastore hibernateDatastore
@Shared PlatformTransactionManager transactionManager
void setupSpec() {
hibernateDatastore = new HibernateDatastore(Person)
transactionManager = hibernateDatastore.getTransactionManager()
}
@Rollback
void "test execute GORM standalone in a unit test"() {
// your logic here
}
}
@Entity
class Person {
...
}
4.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 the save()
method:
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.
The save()
method is defined by the GormEntity trait.
Read
GORM 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 static get(id) method that expects a database identifier to read the Person
object back from the database.
You can also load an object in a read-only state by using the read(id)
method:
def p = Person.read(1)
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 a proxy for an instance by using the load(id)
method:
def p = Person.load(1)
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, change some properties and then call save()
again:
def p = Person.get(1)
p.name = "Bob"
p.save()