Question

I'm having some issues getting the results I want using the criteria builder. Let's say I have a domain object User that has a Company. Now I want to search, with paging:

def c = User.createCriteria()
def results = c.list(max: max, offset: offset) {
    or {
        ilike("name", someSearchTerm)
        ilike("username", someSearchTerm)
        company {
            ilike("name", someSearchTerm)
        }
    }
}

This resulted in using a inner join with the association, which lead me to do:

def c = User.createCriteria()
def results = c.list(max: max, offset: offset) {
    createAlias("company", "c", CriteriaSpecification.LEFT_JOIN)
    or {
        ilike("name", someSearchTerm)
        ilike("username", someSearchTerm)
        ilike("c.name", someSearchTerm)
    }
}

This now works perfectly. However this leads me to the first question. CriteriaSpecification.LEFT_JOIN is deprecated. What should it be replaced with?

Also, part of the paging should be taking ordering into consideration. The documentation clearly states that I can do:

def c = User.createCriteria()
def results = c.list(max: max, offset: offset) {
    createAlias("company", "c", CriteriaSpecification.LEFT_JOIN)
    or {
        ilike("name", someSearchTerm)
        ilike("username", someSearchTerm)
        ilike("c.name", someSearchTerm)
    }
    order("someAttribute", "asc")
}

but this results in an error, stating that order cannot take String, String as arguments. Any ideas?


UPDATE

I have found the issue. I had a variable declared earlier called order that I was using, and the closure was trying to use that instead of the criteria method.

Was it helpful?

Solution

I have found the issue. I had a variable declared earlier called order that I was using, and the closure was trying to use that instead of the criteria method.

OTHER TIPS

You should try this.

def c = User.createCriteria()
def results = c.list(max: max, offset: offset) {
createAlias("company", "c", CriteriaSpecification.LEFT_JOIN)
or {
    ilike("name", someSearchTerm)
    ilike("username", someSearchTerm)
    ilike("c.name", someSearchTerm)
}
order(Order.asc('someAttribute'))
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top