Question

I'm a bit confused about what is happening when I give a Grails dynamic finder the wrong type.

For instance, if I have class Foo

class Foo {
    //long id (implicit id is a long)
    Long barValue
}

And I do, Foo.findByIdAndBarValue('1', '2'), I get a result, but I am confused about what is going on here with the string values.

Is it passing these string values directly to the db (potentially ignoring valuable indices due to type mismatch) or is Grails automatically converting the types?

Was it helpful?

Solution

When passing parameters to the dynamic finders on Grails domain classes the parameters are dynamically typed. This allows for automatic type conversion, by Groovy.

In your example, Groovy is seeing that the barValue is of type Long and is casting the String value to a Long.

JN3015-Types explains this behavior of Groovy a bit further with some examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top