Grails GORM criteria on null association

喜你入骨 提交于 2019-12-23 04:35:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!