I have the following:
class Book
{
static belongsTo = [cart: Cart]
}
When I instantiate Book, does it automatically create an instance of Cart and then attach book to it?
This creates a field in the class of type Cart
with name cart
, as if you had this in your source code:
class Book {
Cart cart
static belongsTo = [cart: Cart]
}
It's a persistent property, like String title
, etc. But since its type is another domain class it's loaded lazily by default (you can configure this in the mapping or per-query). So loading a Book
instance doesn't load its owning Cart
instance, but when you reference one of the cart
field's properties, it will lazily load.
Additionally, since you've declared a belongs-to relationship, when you delete a Cart
, all of its Book
s will be deleted too.