When building Grails applications you have to consider the problem domain you are trying to solve. For example if you were building an
Amazon bookstore you would be thinking about books, authors, customers and publishers to name a few.
These are modeled in GORM as Groovy classes so a
Book
class may have a title, a release date, an ISBN number and so on. The next few sections show how to model the domain in GORM.
To create a domain class you can run the
create-domain-class target as follows:
grails create-domain-class Book
The result will be a class at
grails-app/domain/Book.groovy
:
If you wish to use packages you can move the Book.groovy class into a sub directory under the domain directory and add the appropriate package
declaration as per Groovy (and Java's) packaging rules.
The above 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 LanguageNow that you have a domain class you can define its properties as Java types. For example:
class Book {
String title
Date releaseDate
String ISBN
}
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
releaseDate
maps onto a column
release_date
. The SQL types are auto-detected from the Java types, but can be customized via
Constraints or the
ORM DSL.