在GORM criteriaBuilder中包含最大值和偏移量标准会返回一个错误 [英] Including the max and offset criteria inside GORM criteriaBuilder returns an error
问题描述
if(count == null&& from = null){
creditAdviceList = CreditAdvice.findAll {
ilike('id',%$ idFilter%)
.....
ilike('statusCode',statusCodeFilter)$ b $ ($)
else if(count!= null&& from == null){
creditAdviceList = CreditAdvice.findAll(max:count){
ilike(' id','%$ idFilter%)
.....
ilike('statusCode',statusCodeFilter)
}
}
else if(count = = null&& from!= null){
creditAdviceList = CreditAdvice.findAll(offset:from){
ilike('id',%$ idFilter%)
.. ...
ilike('statusCode',statusCodeFilter)
}
}
else if(count!= null&& from!= null){
creditAdviceList = CreditAdvice.findAll(max:count,offset:from){
ilike('id','%$ idFilter%)
.....
ilike('statusCode',statusCodeFilter)
}
}
如果语句对每个可能的场景都是 order
和 cache
,那么基本上会有16个唯一的 if
语句!
我已经尝试过[更多]更短的代码:
creditAdviceList = CreditAdvice.findAll {
ilike('id',%$ idFilter%)
.....
ilike 'statusCode',statusCodeFilter)
$ b $ if(count!= null){
maxResults(count)
}
if(from!= null){
firstResult(from)
}
}
但它给我一个错误:
...没有方法签名:grails.gorm.DetachedCriteria.maxResults()适用于参数类型:(java.lang.Integer)...
我试图将 offset
到 int
,整数
, code>等等。我还省略了标准内部的
if if
语句,但是同样的e发生错误消息。
DetachedCriteria
,这与您在文档中提到的 createCriteria
的结果不同。如果groovy会找到足够接近的东西,它会在错误信息中告诉你。处理最大/最小需求的最简单方法是使用map(这是通过的第一个参数)。例如:
def qcfg = [:]
if(count){
qcfg.count = count
if(from){
qcfg.offset = from
}
creditAdviceList = CreditAdvice.findAll(qcfg){...}
混合,匹配,提取,缩小,如您所见
Can I make this code shorter?
if(count == null && from = null) {
creditAdviceList = CreditAdvice.findAll {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
else if(count != null && from == null) {
creditAdviceList = CreditAdvice.findAll(max: count) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
else if(count == null && from != null) {
creditAdviceList = CreditAdvice.findAll(offset: from) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
else if(count != null && from != null) {
creditAdviceList = CreditAdvice.findAll(max: count, offset: from) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
You see, its a series if
statement for each possible scenario. Imagine if one would to use also order
and cache
in the parameter- there will be basically 16 unique if
statements!
I've tried this [more] shorter code:
creditAdviceList = CreditAdvice.findAll {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
if(count != null) {
maxResults(count)
}
if(from != null) {
firstResult(from)
}
}
But it gives me an error:
...No signature of method: grails.gorm.DetachedCriteria.maxResults() is applicable for argument types: (java.lang.Integer)...
I tried to convert offset
to int
, Integer
, String
, etc. I also omit the if
statement inside the criteria, but the same error message occur.
findAll
with a closure passed is using a DetachedCriteria
internally, which is not the same as the result you would get from createCriteria
mentioned in the docs. If groovy would find "something close" enough, it would tell you in the error message. The easiest way to deal with your max/from demands would be with simply with a map (which is the first argument passed). E.g.:
def qcfg = [:]
if (count) {
qcfg.count = count
}
if (from) {
qcfg.offset = from
}
creditAdviceList = CreditAdvice.findAll(qcfg) { ... }
mix, match, extract, shorten as you see fit
这篇关于在GORM criteriaBuilder中包含最大值和偏移量标准会返回一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!