I have a class like this:
class Foo {
String bar
}
I am trying to get the IDs of all the Foo objects whose bar String is in the list bars. I have tried it several ways, always receiving the same error:
java.lang.String cannot be cast to java.util.Collection
Some of the things I have tried:
def ids = Foo.findAllByBarInList( bars )*.id
def ids = Foo.findAllByBarInList( bars ).collect{ it.id }
def ids = Foo.findAllByBarInList( bars ).collect{ it -> it?.id }
UPDATE:
I was making bars with split so it was an array, not a list. It threw me off because Foo.findAllByBarInList( bars ) returned my Foo objects just fine, only when I tried to collect the ids did it fail. I now make bars with tokenize instead and all is well.
That works for me as long as
barsis a ListBut if all you want is the
idfield it’s a waste to pull entire domain class instances from the database. For now there’s just the one field, but in practice it will likely be a larger table. So I’d use an HQL query: