views:

36

answers:

1

Mormally setting up one-to-many associations is easy. Take for example:

class Author {         
  String firstName 
  String lastName 

  static hasMany = [books: Book]        

  static constraints = { 
      books(nullable: true) 
  } 
} 

class Book {         
  String title 
  Author author 
  Publisher publisher 

  static constraints = { 
    author(nullable: true) 
    publisher(nullable: true) 
  } 
} 

However, if I've already setup the Author domain without knowing Book at all, there is no static hasMany = [books: Book] specified initially. Later, I want to add a Book domain and want to add static hasMany = [books: Book] to Author. Could I do this with a plugin? If so, how?

Thanks.

A: 

If you don't want to update the Author class, you could create your own association class.

class AuthorsToBooks {
    Author author
    static belongsTo = [Book: book]
}
Blacktiger
thanks for the good one! still, what I need to do just to add "static hasMany = [books: Book] " dynamically to Author class, without creating this new association class?
john
I guess you could add a list of books to you domain class directly and add it to the transients so it doesn't persist.
Blacktiger