【问题标题】:grails search with where on a hasmanygrails在hasmany上的位置搜索
【发布时间】:2014-10-08 08:40:36
【问题描述】:

所以,一直试图在 grails 中运行搜索,但无法弄清楚发生了什么。

该应用程序有两个域,一个国家地图(称为国家),第二个是 regstat,其中包含每个国家/地区的数据。 我设置它以便国家有很多 regstat,并且 regstat 属于国家。 (最后粘贴了定义)。因此,尝试拨打电话,让我根据国家和地区获得 regstat 条目。

    def countdata(String sn, String regsel) {

    def cpp = Regstat.where {
        domain { countryiso == sn }  && reg == regsel
    }

    render cpp as JSON   
}

给我以下错误信息。

    /GenMap/getcountry/countdata
Class
groovy.lang.MissingMethodException
Message
No signature of method: grails.gorm.DetachedCriteria.domain() is applicable for argument types: (genmap.GetcountryController$_countdata_closure1_closure10_closure11) values: [genmap.GetcountryController$_countdata_closure1_closure10_closure11@5dbfaacf] Possible solutions: min(), join(java.lang.String), min(java.lang.String), join(java.lang.String), min(groovy.lang.Closure), min(java.util.Comparator)

我也尝试过使用国家

def countdata(String sn, String regsel) {

    def cpp = Regstat.where {
        country { countryiso == sn }  && reg == regsel
    }

    render cpp as JSON   
}

这给了我以下错误消息(在 JSON 网络返回中)

{"alias":null,"async":{"decorators":[{"class":"com.sun.proxy.$Proxy40"}],"gormOperations":{"_ref":"..","class":"grails.gorm.DetachedCriteria"},"persistentClass":{"internalPromise":{"bindErrorManager":{"bindErrorListeners":[],"class":"groovyx.gpars.dataflow.impl.DataflowChannelEventOrchestrator","listeners":[]},"bound":false,"error":false,"eventManager":{"bindErrorListeners":[],"class":"groovyx.gpars.dataflow.impl.DataflowChannelEventOrchestrator","listeners":[]}

所以有两个域。 一个是国家地图。

    class Country {
    static hasMany = [regstats: Regstat]
    String countryiso
    String countryname
    static constraints = {
        countryiso size:2..2, unique: true, validator:{ it.toUpperCase() == it }
    }
    static mapping = {
        index: 'countryiso'
    }
}

和注册统计

class Regstat {
    static belongsTo = [country: Country]
    String reg
    int status
    String exturl
    Date impdate
    Date lupdate
    String impnote
    static constraints = {
        reg(inList: ["FATCA", "ITC2014", "AEOI"], unique:'country')
        exturl(nullable:true)
        impnote(nullable:true)
        impdate(nullable:true)
    }
    static mapping = {
        index: 'reg'
        impnote type: 'text'
    }
}

第一次回复后更新 也试过了

def countdata(String sn, String regsel) {

def cpp = Regstat.where {
    country.countryiso == sn  && reg == regsel
}

render cpp as JSON   

}

收到以下错误消息(和乱码的 JSON 输出)

| Error 2014-10-08 10:56:24,532 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /GenMap/getcountry/countdata - parameters:
sn: RU
regsel: FATCA
Stacktrace follows:
Message: null
    Line | Method
->>   82 | <init>                  in groovyx.gpars.serial.SerialHandle
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     41 | <init>                  in     ''
|    174 | <init> . . . . . . . .  in groovyx.gpars.serial.SerialHandle$LocalSerialHandle
|    172 | <init>                  in     ''
|    165 | create . . . . . . . .  in groovyx.gpars.serial.SerialHandle
|     62 | getOrCreateSerialHandle in groovyx.gpars.serial.WithSerialId
|    202 | value . . . . . . . . . in grails.converters.JSON
|    162 | convertAnother          in     ''
|    202 | value . . . . . . . . . in     ''
|    162 | convertAnother          in     ''
|    202 | value . . . . . . . . . in     ''
|    162 | convertAnother          in     ''
|    202 | value . . . . . . . . . in     ''
|    134 | render                  in     ''
|    150 | render . . . . . . . .  in     ''
|     15 | countdata               in genmap.GetcountryController$$EOs5m7Ro
|    198 | doFilter . . . . . . .  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter                in grails.plugin.cache.web.filter.AbstractFilter
|   1142 | runWorker . . . . . . . in java.util.concurrent.ThreadPoolExecutor
|    617 | run                     in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . . . . . . . in java.lang.Thread

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    来自documentation

    可以使用点运算符来查询关联,以指定 要查询的关联属性名:

    这可能会起作用:

    def cpp = Regstat.where {
        country.countryiso == sn && reg == regsel
    }
    return cpp.list() as JSON
    

    希望有帮助

    【讨论】:

    • 我也试过了。然后得到一个奇怪的错误(用该错误消息更新了原始帖子)
    • 我刚刚更新了我的答案。第 84 行的例外是关于查询或渲染?
    • 尝试暂时不使用 GPars - 你需要减少变量的数量(尤其是当你无法让单线程查询工作时运行多线程查询......)这样你就可以看到正在发生的事情.
    • 这可能应该是对问题的评论,而不是对这个答案的评论:)
    • 成功了,问题是我发送的是 cpp 而不是 cpp.list()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多