Search code examples
mongodbhibernategrailsgrails-orm

Grails GORM hasMany association inconsistent with MongoDB, When try to fetch child Records,it fetches the record sometimes and doesn't fetch sometimes


Below is the domain class Author which has a one-to-many association with Book. When trying to fetch Author, sometimes will get the associated collection of Book domain object and sometimes returns null. Any thoughts on why it is inconsistent?

class Author { static mapWith = "mongo" String name static hasMany = [books: Book] }

_author.gson

model { Author author } json g.render(author]) { books g.render(author.books) }

Environment details:

grailsVersion=3.3.5 gormVersion=6.1.8.RELEASE

We are using GORM multi-tenancy and using MongoDB database.


Solution

  • I have switched to sub-document model and it's working now. Here is the domain model code.

    class Author {
        static mapWith = "mongo"
        String name
        List<Books> book
        static embedded = ['book'] 
    }