使用 'lower()' 和 'in' 运算符的 Grails gorm 'where' 查询

Grails gorm 'where' query with 'lower()' and 'in' operator

我有一个域class

class Hashtag {
    String tag
}

为什么

Hashtag.where { lower(tag) == "#london" }.list()

工作正常,但是

Hashtag.where { lower(tag) in [ "#london", "#paris" ] }.list()

结果

org.springframework.dao.InvalidDataAccessResourceUsageException: Unsupported function [lower] defined in query for property [hashtag] with type [class java.lang.String]

如何正确编写这样的查询?

谢谢!

我不确定它是否能满足您的需要。 但是 documentation 表示您可以在 where 块中使用子查询,如下所示:

Hashtag.where { lower(tag).of{} in [ "#london", "#paris" ] }.list()

请试一试,如果不行请告诉我。

希望对您有所帮助!

我无法回答为什么将 lower()in() 一起使用不起作用。我读了 source 但我对 AST 的了解还不够深,无法理解它。

但是,要解决您的问题,您可以使用 derived property 来执行 lower()

class Hashtag {
    String tag
    String loweredTag

    static mapping {
        loweredTag formula: 'lower(tag)'
    }
}

然后可以在where查询中使用派生的属性:

Hashtag.where { loweredTag in [ "#london", "#paris" ] }.list()

回答我自己的问题时,我找到了进行 case-insensitive 比较的替代方法 - 使用条件。这种方法的一个优点是 cities 中的值也可以是混合大小写。

def cities = ["#LONdon", "#PAris"]
Hashtag.createCriteria().list {
    or {
        cities.each { eq("tag", it, [ignoreCase: true])
    }
}