Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails createCriteria: finding objects via a field that is a null domain class instance

I'm seeing some unexpected behavior in Grails' createCriteria. I have a domain class that looks like this:

MyDomainClass {
    AnotherDomainClass anotherDomainClass
    static constraints = {
        anotherDomainClass(nullable:true)
    }
}

I want to find all instances of MyDomainClass where anotherDomainClass is null. So I do this:

return MyDomainClass.createCriteria().list {
    eq('anotherDomainClass', null)
}

However, I get nothing back.

What am I doing wrong? I can see there are database entries where the ANOTHERDOMAINCLASS_ID column is indeed null (or blank, I can't tell).

I'd be fine creating a query that references the ANOTHERDOMAINCLASS_ID column directly, but I haven't found a way yet.

Thanks!

like image 335
Dean Moses Avatar asked Apr 03 '11 22:04

Dean Moses


1 Answers

Instead of using eq you can use the isNull

def results = MyDomainClass.withCriteria {
    isNull('anotherDomainClass')
}

Here's a good reference HibernateCriteriaBuilder Javadoc too.

like image 51
Derek Slife Avatar answered Nov 04 '22 00:11

Derek Slife