Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a grails dynamic finder looking for 0 or null

What's the best way to write a Grails dynamic finder that is searching for a flag that equals 0 or is null?

Example

class Book {
    String title
    Date releaseDate
    String author
    Boolean paperback
    Integer isArchived
}

isArchived is nullable. So the query is either looking for isArchived = 0 or if it is null it also wants it in the results.

EDIT: At first glance this question just seems like I am not even trying and didn't glance at the documentation. I assure you that I have been researching and trying many different things.

Also, yes, a Boolean flag in a database should not be nullable, but it is not possible to change the table at this point in time. The developer in this case relied on setting the value of the flag to 0 by default in the domain object. While this worked in the majority of cases, there are certain scenarios where the flag gets set to null.

I have tried many different ways of doing this using dynamic finders but it seems like having the same column twice in a dynamic finder is not possible.

I would accept the answer, "No this is not possible using dynamic finders just use the createCriteria() method." I just want to know for sure if this is not possible.

like image 546
anataliocs Avatar asked Oct 21 '22 19:10

anataliocs


1 Answers

You could use the where feature, if the version of Grails you're using supports it:

def results = Book.where {
    (isArchived == null || isArchived == 0)
}

You could also use HQL:

Book.findAll("from book b where b.isArchived IS NULL or b.isArchived = 0")
like image 120
Gregg Avatar answered Oct 24 '22 00:10

Gregg