Grails CreateCriteria logical OR not working properly

97 Views Asked by At

I'm trying to perform a filter on a group of objects based on a "State" criteria.

...
or {
    office {
        state {
            'in'('abbrev', filters.stateFilter)
        }
    } 
    state {
        'in'('abbrev', filters.stateFilter)
    }
}

If the State filter in the OR is included in the cide it only get objects matching the State and not the Office.State as well. If I remove the State filter code it properly gets the Office.State of the Object.

The criteria needs to get the State for the Object (if it has one) as well as the Office.State of the Object (if it has one).

I'm assuming it has something to do with some implicit joins in the criteria builder?

Any leads would be appreciated!

1

There are 1 best solutions below

0
Vaesive On

Thanks to Jeff in the comments I was able to turn on SQL logging to find my issue resolution with some more Googling. As I suspected, it was performing an inner join. Here is the code that I needed to resolve the issue.

...
createAlias('state', 'state', JoinType.LEFT_OUTER_JOIN)
or {
    office {
        'in'('state.abbrev', filters.stateFilter)
    } 
    'in'('state.abbrev', filters.stateFilter)
}