views:

52

answers:

1

Here's the domain classes I'm working with:

class Foo {
    String name,
           type

    static hasMany = [ bars: Bar ]

    List bars

    static mapping = {
        bars lazy:false
    }
}

class Bar {
    String value
    static belongsTo = Foo
}

I've written some Criteria queries in order to give the users an interface to query from. The interface provides a drop down in order to select a specific value for Bar. The problem happens when a user tries to specify a value for the Bar class, and wants to get the related Foos. Here's the Criteria query:

def query = {
    and {
        if (params.name && params.name != '') {
            ilike('name', "%${params.name}%")
        }
        if (params.type && params.type != '') {
            ilike('type', "%${params.type}%")
        }
        if (params.value && params.value != '') {
            bars {
                eq("value", params.value)  
            }
        }
    }
}

def results = Foo.createCriteria().list(params,query)

Essentially, it looks like what's happening is that only the Bars that match the specified value are being returned with the Foo object, even if the Foo object has other Bars that don't match that object. I want it to return all of the Bars, since I'm querying for Foo's with matching Bars.

Any ideas? Further clarification needed?

+1  A: 

According to grails guide your query looks fine.

I see the problem in Foo object. Try to remove List bars. The relation to Bar is already defined with static hasMany = [ bars: Bar ].

Update

I would start with grails guide. Implement their example. And then try to change it to my solution step by step. You will see where the problem lies.

amra
Well, I wanted bars to be a List, instead of the default java.util.Set.
Pat
See my update ...
amra