(Quick Reference)
Domain Class Usage
A domain class fulfills the M in the Model View Controller (MVC) pattern and represents a persistent entity that is mapped onto an underlying database table. In Grails a domain is a class that lives in the
grails-app/domain
directory. A domain class can be created with the
create-domain-class command:
grails create-domain-class org.bookstore.Book
or with your favourite IDE or text editor.
package org.bookstoreclass Book {
String title
Date releaseDate
Author author
}
The class name, by default, is mapped to the table name in lower case and separated by underscores instead of camel case so a domain class named
BookStore
by default would map to a table named
book_store
. Each property maps to individual columns.
One limitation of the default table naming scheme is that it is problematic to have 2 domain classes with the same name, even if they are defined to be in separate packages. For example,
com.bookstore.BookStore
and
com.publishing.utility.BookStore
would each map to a table named
book_store
. If the 2 classes are defined in the application this problem can be managed by giving the classes different names or by providing a specific table name for one or both of the classes that deviates from the default (see the
ORM DSL section of the user guide for more details). If one or both of the domain classes is provided by a plugin the application author may not have access to those options. To help manage a situation like that, GORM may be configured to prefix table names with plugin names by default. For example, if the
com.publishing.utility.BookStore
domain class is provided by a plugin named
PublishingUtilities
, the default table name could be
publishing_utilities_book_store
. To enable that behavior the
grails.gorm.table.prefix.enabled
config property must be set to true. Example:
// grails-app/conf/Config.groovygrails.gorm.table.prefix.enabled = true
Refer to the
configuration section of more details on defining configuration options.
Refer to the user guide section on
GORM for more information.