Search code examples
hibernategrailsgrails-orm

How to get all attributes of a object mapped inside another with Grails


I have the code below in Grails:

class Person{
    String name

    Address address

}

class Address{

    String addressDescription

}

Address addressX = new Address(addressDescription:"Avenue xxx");

address.save(); 

Person p = new Person(name:"MyName",address:addressX);

p.save();

So, after the objects have been saved, I tried to get person like this:

Person p = Person.findByName("MyName");

but, when a try get p.getAddress() I get only the Address ID, is that correct? Is there a way to bring all the properties automatically with the object person ?

I know that a can do Address.findById(p.address.id) but I don't want make more queries.

Thanks for help!


Solution

  • Collections are generally lazy loaded by default. When you look at the data through a debugger, you will generally not see any of the lazy loaded properties. However, once you access them, they will be there:

    p.address.shortDescription