views:

397

answers:

3

Hi guys,

I have such domain classes:

class ServicesGroup {
    Long id
    String name
    String description

    String toString(){
        return name
    }

    static mapping = {
        version false
        table 'root.services_groups'

        id column:'group_id' 
        name column:'group_name'
        description column:'group_desc'
    }
}

and

class Step {
    Long id
    ServicesGroup service
    String stepType
    Integer stepFrom
    Integer stepTo

    static constraints = {
        stepType(inList:['operator', 'client'])
    }

    static mapping = {
        version false
        table 'bill.steps'
        service column:'service_group_id'
    }
}

The relationship is - one ServicesGroup entry can have multiple Step instances.

However, when in my controller I try to

Step.findByService(3)

I get:

"org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: Step.findByService() is applicable for argument types: (java.lang.Integer) values: {3}"

However, when I change Step domain class field

ServicesGroup service

to simply

Long service

it works.

What's going on here?

A: 

Try

grails clean
grails run-app

Then try again.

codehead
Nope, tried that. Thanks though.
Karolis
+3  A: 

Try it that way:

Step.findByService(ServicesGroup.get(3))
Siegfried Puchbauer
That works! Can you give my some pointers as to why?Something in the lines of.. service field is declared as ServicesGroup object, therefore if I want to search for stuff using service, I must use the object of given type?Is there any more direct way of achieving the same result without nesting findBy*/get?Thanks!
Karolis
See other answer below. A third way would be to use Hibernate criteria and do Step.withCriteria { service { eq("id", 3) } }.
John Stoneham
+1  A: 

Something like Step.findByService([id: 3]) may work. It only cares about the ID anyway for the purposes of the SQL generation. In a lot of cases like this you can toss a fake map into there rather than the real thing, and save yourself some performance.

On the other hand, the abstraction breaks down a bit when you do this.

John Stoneham