【问题标题】:Grails executeQuery giving me "no such property" exceptionGrails executeQuery 给了我“没有这样的属性”异常
【发布时间】:2012-05-11 01:43:40
【问题描述】:

我有一个名为 Event 的域类:

class Event{
    String eventID // an ID for the event, (there are multiple events with same eventID)
    .....
}

在我的 eventService 类中,我想获取具有不同 eventID 的所有事件,所以我有以下查询:

Event.executeQuery("select distinct e.eventID from Event e", [max: max, offset: offset])

根据the grails docs 它应该可以工作。但是,我收到了这个错误:

| Error 2012-05-10 18:14:09,643 [http-bio-8080-exec-9] ERROR errors.GrailsExceptionResolver  - MissingPropertyException occurred when processing request: [POST] /events/event/list - 
No such property: id for class: java.lang.String. Stacktrace follows:
Message: No such property: id for class: java.lang.String

Line | Method
->>   35 | run                 in C__src_Event_events_grails_app_views_event__List_gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|     18 | render . . . . . .  in org.events.EventController
|     67 | list . . . . . . .  in     ''
|   1110 | runWorker           in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run                 in java.lang.Thread

我对 grails 还很陌生,如果有任何帮助,我将不胜感激。顺便说一句,我使用的是 Grails 2.0.1。

【问题讨论】:

    标签: grails hql grails-orm


    【解决方案1】:

    问题在于,通过包含分页参数(最大值和偏移量),您正在触发框架尝试创建 PagedResultList。

    PagedResultList 期望保存具有 id 的域对象。

    但是,您的 select 语句返回字符串。因此,String类没有id属性的消息。

    如果你删除分页参数,它应该可以工作,你应该只得到你的字符串列表。

    【讨论】:

    • 感谢您的回答 GreyBeardGeek。但根据 grails 文档link(从上数第三个),这应该正常工作吗?
    • 您的查询没有任何位置参数。您在该页面上被指出的查询具有位置参数。
    • try Event.executeQuery("select distinct e.eventID from Event e", null, [max: max, offset: offset])
    【解决方案2】:

    我想出了答案,有两种方法可以做到这一点。 GreyBeardedGeek 的回答部分正确。

    第一种方法

    def eveIdList = Event.executeQuery("select distinct e.eventID from Event e", [max: max, offset: offset])
    

    第二种方法 获取 eventID 列表:

    def eveIdList = Event.createCriteria().list{
        projections{
            distinct("eventID")
        }
        firstResult(offset?:0)
        maxResults(max?:10)
    }
    

    我得到了长度 = 最大值和偏移量的字符串列表。我收到错误是因为我试图为每个 eventID 字符串执行 String.id 。为了获取具有相应 eventID 的域对象,我这样做:

    def eveList = []
    eveIdList.each{
        eveList << Event.findByEventID(it as String)
    }
    

    我的问题

    • 它们效率高吗?
    • 一种方法比另一种更有效吗?
    • 有没有更好的方法来解决这个问题?

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 2012-12-25
      • 2019-12-30
      • 2011-04-15
      • 2023-03-22
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多