Search code examples
springgrailsgrails-ormgrails-2.0

Grails ManyToOne Relationship auto-insert to List?


I have the following Grails classes and relationships:

class Book {
   static hasMany = [articles: Article]
}

class Article {
   static belongsTo = [book: Book]
}

Can I do the following to get a correct relationship being set?

def book = // some book instance
def article = new Article()
article.book = book
article.save()

Is the former same as the following?

def book = // some book instance
def article = new Article()
book.addToArticles(article)
book.save()

Solution

    1. Yes you can.
    2. Yes it is.

    you can turn on sqllog in DataSource.config and compare result sql. belongsTo - saves and deletes will cascade from Book to the associated Article.