<commit_before>
===== Lazy Collections


As discussed in the section on <<fetching,Eager and Lazy fetching>>, GORM collections are lazily loaded by default but you can change this behaviour with the ORM DSL. There are several options available to you, but the most common ones are:

* lazy: false
* fetch: 'join'

and they're used like this:

[source,java]
----
class Person {

    String firstName
    Pet pet

    static hasMany = [addresses: Address]

    static mapping = {
        addresses lazy: false
        pet fetch: 'join'
    }
}
----

[source,java]
----
class Address {
    String street
    String postCode
}
----

[source,java]
----
class Pet {
    String name
}
----

The first option, `lazy: false` , ensures that when a `Person` instance is loaded, its `addresses` collection is loaded at the same time with a second SELECT. The second option is basically the same, except the collection is loaded with a JOIN rather than another SELECT. Typically you want to reduce the number of queries, so `fetch: 'join'` is the more appropriate option. On the other hand, it could feasibly be the more expensive approach if your domain model and data result in more and larger results than would otherwise be necessary.

For more advanced users, the other settings available are:

* `batchSize: N`
* `lazy: false, batchSize: N`

where N is an integer. These let you fetch results in batches, with one query per batch. As a simple example, consider this mapping for `Person`:

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

    String firstName
    Pet pet

    static mapping = {
        pet batchSize: 5
    }
}
----
If a query returns multiple `Person` instances, then when we access the first `pet` property, Hibernate will fetch that `Pet` plus the four next ones. You can get the same behaviour with eager loading by combining `batchSize` with the `lazy: false` option.

You can find out more about these options in the http://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#fetching[Hibernate user guide]. Note that ORM DSL does not currently support the "subselect" fetching strategy.


===== Lazy Single-Ended Associations


In GORM, one-to-one and many-to-one associations are by default lazy. Non-lazy single ended associations can be problematic when you load many entities because each non-lazy association will result in an extra SELECT statement. If the associated entities also have non-lazy associations, the number of queries grows significantly!

Use the same technique as for lazy collections to make a one-to-one or many-to-one association non-lazy/eager:

[source,java]
----
class Person {
    String firstName
}
----

[source,java]
----
class Address {

    String street
    String postCode

    static belongsTo = [person: Person]

    static mapping = {
        person lazy: false
    }
}
----

Here we configure GORM to load the associated `Person` instance (through the `person` property) whenever an `Address` is loaded.


===== Lazy Single-Ended Associations and Proxies


Hibernate uses runtime-generated proxies to facilitate single-ended lazy associations; Hibernate dynamically subclasses the entity class to create the proxy.

Consider the previous example but with a lazily-loaded `person` association: Hibernate will set the `person` property to a proxy that is a subclass of `Person`. When you call any of the getters (except for the `id` property) or setters on that proxy, Hibernate will load the entity from the database.

Unfortunately this technique can produce surprising results. Consider the following example classes:

[source,java]
----
class Pet {
    String name
}
----

[source,java]
----
class Dog extends Pet {
}
----

[source,java]
----
class Person {
    String name
    Pet pet
}
----

and assume that we have a single `Person` instance with a `Dog` as the `pet`. The following code will work as you would expect:
[source,groovy]
----
def person = Person.get(1)
assert person.pet instanceof Dog
assert Pet.get(person.petId) instanceof Dog
----

But this won't:

[source,groovy]
----
def person = Person.get(1)
assert person.pet instanceof Dog
assert Pet.list()[0] instanceof Dog
----

The second assertion fails, and to add to the confusion, this will work:

[source,groovy]
----
assert Pet.list()[0] instanceof Dog
----

What's going on here? It's down to a combination of how proxies work and the guarantees that the Hibernate session makes. When you load the `Person` instance, Hibernate creates a proxy for its `pet` relation and attaches it to the session. Once that happens, whenever you retrieve that `Pet` instance with a query, a `get()`, or the `pet` relation _within the same session_ , Hibernate gives you the proxy.

Fortunately for us, GORM automatically unwraps the proxy when you use `get()` and `findBy\*()`, or when you directly access the relation. That means you don't have to worry at all about proxies in the majority of cases. But GORM doesn't do that for objects returned with a query that returns a list, such as `list()` and `findAllBy\*()`. However, if Hibernate hasn't attached the proxy to the session, those queries will return the real instances - hence why the last example works.

You can protect yourself to a degree from this problem by using the `instanceOf` method by GORM:

[source,java]
----
def person = Person.get(1)
assert Pet.list()[0].instanceOf(Dog)
----

However, it won't help here if casting is involved. For example, the following code will throw a `ClassCastException` because the first pet in the list is a proxy instance with a class that is neither `Dog` nor a sub-class of `Dog`:

[source,java]
----
def person = Person.get(1)
Dog pet = Pet.list()[0]
----

Of course, it's best not to use static types in this situation. If you use an untyped variable for the pet instead, you can access any `Dog` properties or methods on the instance without any problems.

These days it's rare that you will come across this issue, but it's best to be aware of it just in case. At least you will know why such an error occurs and be able to work around it.<commit_msg>Update docs about proxies and inheritance<commit_after>
===== Lazy Collections


As discussed in the section on <<fetching,Eager and Lazy fetching>>, GORM collections are lazily loaded by default but you can change this behaviour with the ORM DSL. There are several options available to you, but the most common ones are:

* lazy: false
* fetch: 'join'

and they're used like this:

[source,java]
----
class Person {

    String firstName
    Pet pet

    static hasMany = [addresses: Address]

    static mapping = {
        addresses lazy: false
        pet fetch: 'join'
    }
}
----

[source,java]
----
class Address {
    String street
    String postCode
}
----

[source,java]
----
class Pet {
    String name
}
----

The first option, `lazy: false` , ensures that when a `Person` instance is loaded, its `addresses` collection is loaded at the same time with a second SELECT. The second option is basically the same, except the collection is loaded with a JOIN rather than another SELECT. Typically you want to reduce the number of queries, so `fetch: 'join'` is the more appropriate option. On the other hand, it could feasibly be the more expensive approach if your domain model and data result in more and larger results than would otherwise be necessary.

For more advanced users, the other settings available are:

* `batchSize: N`
* `lazy: false, batchSize: N`

where N is an integer. These let you fetch results in batches, with one query per batch. As a simple example, consider this mapping for `Person`:

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

    String firstName
    Pet pet

    static mapping = {
        pet batchSize: 5
    }
}
----
If a query returns multiple `Person` instances, then when we access the first `pet` property, Hibernate will fetch that `Pet` plus the four next ones. You can get the same behaviour with eager loading by combining `batchSize` with the `lazy: false` option.

You can find out more about these options in the http://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#fetching[Hibernate user guide]. Note that ORM DSL does not currently support the "subselect" fetching strategy.


===== Lazy Single-Ended Associations


In GORM, one-to-one and many-to-one associations are by default lazy. Non-lazy single ended associations can be problematic when you load many entities because each non-lazy association will result in an extra SELECT statement. If the associated entities also have non-lazy associations, the number of queries grows significantly!

Use the same technique as for lazy collections to make a one-to-one or many-to-one association non-lazy/eager:

[source,java]
----
class Person {
    String firstName
}
----

[source,java]
----
class Address {

    String street
    String postCode

    static belongsTo = [person: Person]

    static mapping = {
        person lazy: false
    }
}
----

Here we configure GORM to load the associated `Person` instance (through the `person` property) whenever an `Address` is loaded.


===== Lazy Associations and Proxies


Hibernate uses runtime-generated proxies to facilitate single-ended lazy associations; Hibernate dynamically subclasses the entity class to create the proxy.

Consider the previous example but with a lazily-loaded `person` association: Hibernate will set the `person` property to a proxy that is a subclass of `Person`. When you call any of the getters (except for the `id` property) or setters on that proxy, Hibernate will load the entity from the database.

Unfortunately this technique can produce surprising results. Consider the following example classes:

[source,java]
----
class Pet {
    String name
}
----

[source,java]
----
class Dog extends Pet {
}
----

[source,java]
----
class Person {
    String name
    Pet pet
}
----

Proxies can have confusing behavior when combined with inheritance. Because the proxy is only a subclass of the parent class, any attempt to cast or access data on the subclass will fail. Assuming we have a single `Person` instance with a `Dog` as the `pet`.

The code below will not fail because directly querying the `Pet` table does not require the resulting objects to be proxies because they are not lazy.

[source,groovy]
----
def pet = Pet.get(1)
assert pet instanceof Dog
----

The following code will fail because the association is lazy and the `pet` instance is a proxy.

[source,groovy]
----
def person = Person.get(1)
assert person.pet instanceof Dog
----

If the only goal is to check if the proxy is an instance of a class, there is one helper method available to do so that works with proxies. Take special care in using it though because it does cause a call to the database to retrieve the association data.

[source,groovy]
----
def person = Person.get(1)
assert person.pet.instanceOf(Dog)
----

There are a couple of ways to approach this issue. The first rule of thumb is that if it is known ahead of time that the association data is required, join the data in the query of the `Person`. For example, the following assertion is true.

[source,groovy]
----
def person = Person.where { id == 1 }.join("pet").get()
assert person.pet instanceof Dog
----

In the above example the `pet` association is no longer lazy because it is being retrieved along with the `Person` and thus no proxies are necessary. There are cases when it makes sense for a proxy to be returned, mostly in the case where its impossible to know if the data will be used or not. For those cases in order to access properties of the subclasses, the proxy must be unwrapped. To unwrap a proxy inject an instance of link:../api/org/grails/datastore/mapping/proxy/ProxyHandler.html[ProxyHandler] and pass the proxy to the `unwrap` method.

[source,groovy]
----
def person = Person.get(1)
assert proxyHandler.unwrap(person.pet) instanceof Dog
----

For cases where dependency injection is impractical or not available, a helper method link:../api/org/grails/orm/hibernate/cfg/GrailsHibernateUtil.html#unwrapIfProxy(java.lang.Object)[GrailsHibernateUtil.unwrapIfProxy(Object)] can be used instead.

Unwrapping a proxy is different than initializing it. Initializing a proxy simply populates the underlying instance with data from the database, however unwrapping a returns the inner target.

{'fraction_non_alphanumeric': 0.07840918276614664, 'fraction_numerical': 0.0012125131355589685, 'mean_word_length': 3.738414400612792, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1565344', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3212, 'n_words': 1732}