2011-05-10 98 views
1

問候,我想命令急切地獲取另一個域類以1:m關係擁有的域對象,但不知道如何執行此操作。當我嘗試使用簡化的項目時,出現錯誤。下面是我的嘗試:在Grails中,我如何訂購渴望獲取的域記錄?

class Picture { 
    String name 

    static hasMany = [comments:Comment] 
    static mapping = { 
     comments(lazy:false, sort:'content', order:'desc') 
    } 
} 

class Comment { 
    String content 
    Date dateCreated 

    static belongsTo = [Picture] 
} 

現在,測試紀錄println Picture.get(1) as JSON如何獲取時,我得到以下錯誤:

java.sql.SQLException: Column not found: COMMENTS0_.CONTENT in statement [select comments0_.picture_comments_id as picture1_0_, comments0_.comment_id as comment2_0_ from picture_comment comments0_ where comments0_.picture_comments_id=? order by comments0_.content desc

沒有sort:'content', order:'desc',註釋是隨機的順序,但沒有錯誤。

+0

如果你這樣寫:static mapping = {comments sort:'content',order:'desc'}沒有急切的加載。這不行嗎? – superbly 2011-05-10 06:59:39

+0

我沒有嘗試過,但是當我這樣做時,我仍然得到了與之前相同的SQLException。 – Nenotlep 2011-05-10 07:07:36

+2

我想你遇到了一個已知的問題。 http://jira.grails.org/browse/GRAILS-4089。 – superbly 2011-05-10 07:08:25

回答

0

在孩子的名單已被提取之後,我在代碼中需要他們的時候,我最終做了排序。事情是這樣的:

def sortedComments = myPicture.comments.sort{it.content}.reverse() 

具有允許無限制排序配置即可,而不只是因爲默認情況下檢索到的一個優勢。

+0

我將此標記爲解決方案,因爲它已經這麼久了,似乎沒有任何其他解決方案。我不知道這是否會影響Grails 1.3.7,我沒有時間去研究。我使用了與此非常相似的解決方案來解決問題。 – Nenotlep 2012-02-29 13:51:12