grails create-domain-class org.bookstore.Book
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:
or with your favourite IDE or text editor.
package org.bookstore
class 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.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.
Spring Autowiring of Domain Instances
Since Grails 3.2.8, Spring autowiring of domain instances has been disabled by default because it represents a performance bottleneck. If you are autowiring services into your domain instances, you will need to re-enable it.
For versions of Grails 3.3.0 or above, you can turn on autowire on a single domain class:
class Book {
BookService bookService
String name
static mapping = {
autowire true
}
...
..
.
}
You can turn on autowire for all your domain classes using the Default Mapping setting:
grails.gorm.default.mapping = {
autowire true
}
For versions below Grails 3.3.0, you can re-enable it changing the grails.gorm.autowire
configuration parameter.
grails:
gorm:
autowire: true
if Spring autowiring of domain instances is enabled, read performance will degrade. |