一类产品:
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
}
}
我想找到所有产品,其中productId包含子字符串“filter”。
我写了下一个代码:
Product.findAll {it.productId.contains(filter)}
但这是行不通的。为什么?
最佳答案
这根本不起作用!
您在这里有2个选择:
1)您使用适当的GORM
技术,例如criteria 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`
2)或在grails应用程序的内存(而不是数据库)中对整个数据集使用过滤-不建议:
Product.list().findAll{ it.productId.contains(filter) }
关于grails - GORM findAll不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25399858/