I have a postgresql database that has this column structure:
Author
id
name
Book
id
name
author_id
And Groovy Domain Classes that repressent those tables:
class Author {
static hasMany = [ books : Book ]
Integer id
String name
}
class Book {
static belongsTo = Author
Integer id
Integer project_id
String name
}
My main goal to get a list of books from a author instance.
author = Author.get( 1 ) // gets a author
author.books // must return a list of books.
But this does not work. Is there something glaringly obvious I’m doing wrong?
note I’ve got lots of Ruby/Rails experience and zull Java/Groovy experience.
Change your
Bookclass to:If you don’t specify the
mappinglike that, GORM will create, resp., expect, a JOIN table by default.(BTW, domain classes are automatically provided with a “virtual”
idproperty (of typeLongI think, translating tobigintin PostgreSQL). It’s not necessary to specify it manually, but it also won’t harm.)EDIT: Updated as per the questioners comments:
If a book can really have just one author, you’d state in the
Bookclass:The GORM documentation on one-to-many relations can be found here.