GORM findAll不起作用

Pav*_*hyk 1 grails grails-orm

有类产品:

class Product {
    static hasMany = [attributeValues: ProductAttributeValue]

    String productId
    String manufacturer
    BigDecimal price

    static constraints = {
        productId unique: true, blank: false
        manufacturer blank: false
        price min: BigDecimal.ZERO
    }
}
Run Code Online (Sandbox Code Playgroud)

我想找到所有产品,哪个productId包含子串'filter'.我写了下一个代码:

Product.findAll {it.productId.contains(filter)}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.为什么?

inj*_*eer 5

这应该不起作用!你有两个选择:

1)你使用GORMcriteria query或的方法技术HQL,看起来像:

Product.findAllByProductIdIlike( "%${filter}%" ) // dyn-finders
Product.withCriteria{ ilike 'productId', "%${filter}%" } // criteria    
Product.findAll{ ilike 'productId', "%${filter}%" } // criteria
Product.findAll( "from Product where productId like '%?%'", [ filter ] ) // hql`
Run Code Online (Sandbox Code Playgroud)

2)或者在grails app的内存中使用过滤整个数据集(而不是db) - 不推荐:

Product.list().findAll{ it.productId.contains(filter) }
Run Code Online (Sandbox Code Playgroud)