////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


=== 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 link:{commandLineRefFromRef}create-domain-class.html[create-domain-class] command:

[source,groovy]
----
grails create-domain-class org.bookstore.Book
----

or with your favourite IDE or text editor.

[source,groovy]
----
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  link:{hibernate5GuideFromRef}index.html#ormdsl[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:


[source,groovy]
.grails-app/conf/runtime.groovy
----

grails.gorm.table.prefix.enabled = true
----

Refer to the link:{guidePath}conf.html[configuration] section of more details on defining configuration options.

Refer to the user guide section on link:{guidePath}GORM.html[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:

[source, groovy]
.grails-app/domain/demo/Book.groovy
----
class Book {
    BookService bookService

    String name

   static mapping = {
       autowire true
   }
   ...
   ..
   .
}
----

You can turn on autowire for all your domain classes using the link:{hibernate5GuideFromRef}index.html#_the_default_mapping_constraints[Default Mapping] setting:

[source, groovy]
.grails-app/conf/runtime.groovy
----
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.

[source, yaml]
.grails-app/conf/application.yml
----
grails:
    gorm:
        autowire: true
----

NOTE: if Spring autowiring of domain instances is enabled, read performance will degrade.


