GORM
Domain classes are linked together through relationships; one-to-one, one-to-many, or many-to-many.
Each property is mapped to a
column in the database, where the convention for column names is all
lower case separated by underscores. For example
Domain classes are linked together through relationships; one-to-one, one-to-many, or many-to-many.
GORM is Grails' object relational
mapping (ORM) implementation. Under the hood it uses Hibernate 3.
CRUD
Create
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
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()
Domain Modelling in GORM
To create a domain class you run the create-domain-class command as follows:
grails create-domain-class org.bookstore.Book
The result will be a class at
grails-app/domain/org/bookstore/Book.groovy
:package org.bookstoreclass Book { }
This class will map automatically to a table in the database called
book
(the same name as the class). This behaviour is customizable through the ORM Domain Specific Language
Now that you have a domain class you can define its properties as Java types. For example:
package org.bookstoreclass Book { String title Date releaseDate String ISBN }
releaseDate
maps onto a column release_date
.
No comments:
Post a Comment