Josh K
Josh K

Reputation: 28883

Grails automatic relational fetching

Is is possible to do automatic relation fetching in GORM / Grails?

class Person {
    static hasMany = [cars : Car]
}
class Car {
    static belongsTo = [owner : Person]
}

Then use this relation like:

person = Person.get(1);
person.cars.each() { print it; }

Upvotes: 2

Views: 861

Answers (2)

Burt Beckwith
Burt Beckwith

Reputation: 75671

You can enable eager fetching this way:

static mapping = {
   cars fetch: 'join'
}

See http://grails.org/doc/latest/ref/Database%20Mapping/fetch.html

Upvotes: 3

Hoàng Long
Hoàng Long

Reputation: 10848

The answer is: Yes, that works.

But I recommend reading the GORM Gotchas, to fully understand the basics of Hibernate under Grails' hood. Or sometimes you will see "surprisingly" behavior.

Upvotes: 0

Related Questions