关于null关联的Grails GORM标准 [英] Grails GORM criteria on null association

查看:87
本文介绍了关于null关联的Grails GORM标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关联域对象的域对象,我希望能够作为查询的一部分进行搜索。

使用Book模型作为例子,我会有:

  public class Book {
作者作者
字符串标题
}

公共类作者{
字符串名称
}

..并且想过滤为:

  def book = Book.withCriteria {
或{
ilike(title,%+ params.filter +%)
author {
ilike(name,%+ params.filter +%)
}
}
}

我遇到的问题是,如果我在查询中包含作者,那么即使标题匹配,具有空作者的任何Books也不会被返回。 p>

解决方案

这可能是由于在Book和Author之间创建了隐式内部连接。尝试使用如下的左外连接:

  def book = Book.withCriteria {
createAlias'author',' auth',org.hibernate.sql.JoinType.LEFT_OUTER_JOIN
或{
ilike(title,%+ params.filter +%)
ilike(auth.name, %+ params.filter +%)
}
}


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.

解决方案

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 + "%")
    }
}

这篇关于关于null关联的Grails GORM标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆