【问题标题】:Grails named query for NOT IN用于 NOT IN 的 Grails 命名查询
【发布时间】:2012-04-17 16:04:27
【问题描述】:

试图想出一种简洁的方法来创建带有 NOT IN 子句的命名查询。限制类似乎没有“不在”构造。有谁知道这样做的好方法吗?

解决方案

static namedQueries = {
        activeOnly {
            eq 'active', true
        }
        open {
            ne 'state', this.STATE_COMPLETED
        }
        my { user ->
            or {
                eq 'createdBy', user
                eq 'reviewer', user
                eq 'assignee', user
                eq 'requestedFor', user
                ilike 'notifyList', "%$user%"
            }
        }
        topLevel {
            not {'in'('type', [RequestType.Part])}
        }
    }

注意:“in”这个词必须被引用,因为它是保留的。

这些命名查询可以这样使用:

Request.activeOnly.open.topLevel.list(params)

【问题讨论】:

    标签: hibernate grails criteria named-query


    【解决方案1】:

    可能是这样的:

    Book.findAll("from Book b where b.author not in (?)", ['Borges', 'Sabato', 'Cortazar'])
    

    或使用标准

    Book.withCriteria {
        not {
            'in'('author', ['Borges', 'Sabato', 'Cortazar'])
        }
    }
    

    http://grails.org/doc/1.3.x/ref/Domain%20Classes/createCriteria.html - 'in' 标准方法的文档也有一个链式 'not' 方法。

    【讨论】:

    • 感谢您的回复。这可能有效,但不是命名查询。命名查询使用条件 api 创建和缓存准备好的语句。
    • 感谢您的宝贵时间。更正了语法。 inList 保留用于约束,但引用 'in' 有效。
    猜你喜欢
    • 2016-01-14
    • 2016-01-12
    • 1970-01-01
    • 2015-09-28
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多