0

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])

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 を使用しています。

4

2 に答える 2

2

問題は、ページングパラメーター(maxとoffset)を含めることにより、フレームワークがPagedResultListの作成を試みるようにトリガーしていることです。

PagedResultListは、IDを持つドメインオブジェクトを保持することを想定しています。

ただし、selectステートメントは文字列を返します。したがって、Stringクラスにid属性がないというメッセージ。

ページングパラメータを削除すると、機能するはずであり、文字列のリストを取得するだけです。

于 2012-05-11T02:55:12.907 に答える
0

私は答えを見つけ出し、これを行うには2つの方法があります。GreyBeardedGeek の回答は部分的に正しいものでした。

最初の方法

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

2 番目の方法 eventID のリストを取得します。

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

length = max および offset の文字列のリストを取得しました。eventID 文字列ごとに String.id を実行しようとしたため、エラーが発生しました。対応する eventID を持つドメイン オブジェクトを取得するために、次のようにしています。

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

私が持っている質問

  • それらは効率的ですか?
  • ある方法は他の方法よりも効率的ですか?
  • この問題を解決するより良い方法はありますか?
于 2012-05-11T18:50:52.287 に答える