Search code examples
grailsnullgrails-orm

Is there a way to do null safe GORM dynamic finders?


Is there a way ensure null safety in a GORM dynamic finder?

If I wanted to do something like Book.findByName(author.bookname), I could guard against null authors by changing it to Book.findByName(author?.bookname) but how do I avoid an exception on nulls?

Book.findByName() is applicable for argument types: () values: []

Solution

  • auther?.bookname ? Book.findByName(author.bookname) : null //or do nothing

    Based on the fact from the example that bookname is NOT NULL.

    or you can use find

    Book.find {name == author?.bookname} //Returns null if no records found