Nathan H
Nathan H

Reputation: 49451

Grails hasMany Criteria with 0 values

I have a domain class linked as such:

class Item{ 
static hasMany = [children:Item] 
Item parent 
} 

We are dealing with existing data (not created by grails). One important detail is that if an Item has no parent, the column "parentid" shows "0", not null. To deal with this I had to add:

columns{ 
parent column: 'parentid', ignoreNotFound: true 
}

So far so good. Now the problem arises when I want to write a Criteria search for items WITH NO PARENTS. That is, items with parentid=0.

I tried eq('parent', 0) but it crashed (casting exceptions, mismatch, etc ...), because I assume it expects an object.

I tried eq('parent', null) but it returned no results, because I there are no NULL items.

What's the best way to handle that?

Upvotes: 0

Views: 676

Answers (1)

fabien7474
fabien7474

Reputation: 16576

Did you try : eq('parent.id', 0) ?

Upvotes: 1

Related Questions