Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: what is the return type of DomainClass.findAllBy*?

Tags:

grails

grails has documentation for findAllBy*, but it doesn't tell you what it returns, or how to use it. E.g.

def results = Book.findAllByTitleLike("%Hobbit%)

is results a map? a set? a List? a special object with paging and extra features, and contains a collection of Books?

intellij has no clue what the type or results is (it thinks its a single Book) , the official grails documentation doesn't tell you, Google returns no examples (that I can find), so how could one find such info out?

What is returned if no books are found? Null? and empty collection of some sort? how does one safely determine the number of books found? how does one access the returned books? Are they ordered?

like image 898
John Little Avatar asked Jan 09 '23 00:01

John Little


1 Answers

findAllBy returns:

  • an ArrayList of Books, if found.
  • an empty ArrayList, if not found.

findBy (which is designed to return single items) returns:

  • a Book, if found
  • null when empty

If you're curious about object return types in Groovy, you can use the Apache Commons ReflectionToStringBuilder to show you return types:

import org.apache.commons.lang.builder.ReflectionToStringBuilder
...
log.error (new ReflectionToStringBuilder(results).toString())
like image 81
ThisIsMatt Avatar answered Feb 21 '23 17:02

ThisIsMatt