Search code examples
grailsone-to-manyeagerempty-list

Grails Eager fetching an empty association returns null


I have domain classes as follows

Class Author{
String name 

List books = LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(Book.class)


static hasMany = [books:Book]

}

Class Book {
String title
static belongsTo = [author:Author]

}

Now i am trying to fetch Author

Author authorInstance = Author.find("from Author a inner join fetch a.books where a.id =:authorid",[authorid:Long.parseLong(params.id)]

Now, when this author doesn't have any books, i.e books association is empty authorInstance returned is null

I am not sure, but i think this is happening because of lazyList(the reason i am using lazyList is to have easier data binding).


Solution

  • Figured out the problem is with inner join, when association is empty inner join would obviously not return anything, so i changed that to left outer join, and it started working