////
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.
////


== fetch



=== Purpose


Configures the fetching behavior of an association.


=== Examples


[source,groovy]
----
class Author {

    static hasMany = [books: Book]

    static mapping = {
        books fetch: 'join'
    }
}
----


=== Description


Usage: `association_name(fetch:string)`

Arguments:

* `fetchStrategy` - The fetching strategy to use. Either `join` or `select`.

By default GORM assumes fetching of associations is done using a `SELECT` when the association is accessed. If you prefer that the association be fetched eagerly at the same time then you can override the behavior:

[source,groovy]
----
class Author {

    static hasMany = [books: Book]

    static mapping = {
        books fetch: 'join'
    }
}
----

Here the `books` association will be fetched using a join at the same time the author is retrieved, for example:

[source,groovy]
----
def author = Author.get(1)
// the books collection is pre-initialized - no risk of lazy loading exceptions
----

Note that excessive use of joins can be a performance bottleneck. See the section on  link:{hibernate5Guide}/manual/index.html#fetching[Eager vs Lazing Fetching] in the user guide.
