问题
I have a domain object with an associated domain object which I'd like to be able to search on as part of a query.
Using the Book model as an example, I'd have:
public class Book{
Author author
String title
}
public class Author{
String name
}
.. and want to filter like:
def book = Book.withCriteria{
or{
ilike(title, "%" + params.filter + "%")
author{
ilike("name", "%" + params.filter + "%")
}
}
}
The problem I'm having is that if I include author in the query, then any "Books" with a null author will not be returned, even if the title matches.
回答1:
That's likely due to the implicit inner join being created between Book and Author. Try using a left outer join like this:
def book = Book.withCriteria{
createAlias 'author', 'auth', org.hibernate.sql.JoinType.LEFT_OUTER_JOIN
or{
ilike(title, "%" + params.filter + "%")
ilike("auth.name", "%" + params.filter + "%")
}
}
来源:https://stackoverflow.com/questions/32751113/grails-gorm-criteria-on-null-association