我對標準構建器有一個令人沮喪的問題。我有一個應用程序,其中一個用戶有一個日曆,而日曆有很多條目。看起來很簡單,但是當我嘗試獲取給定用戶的日曆條目時,我無法訪問用戶屬性(MissingMethodException)。下面的代碼:Grails:標準構建器中存在嵌套關聯的問題
def getEntries(User user) {
def entries = [ClassName].createCriteria().list() {
calendar {
user {
eq("id", user.id)
}
}
}
}
我已經甚至嘗試以下變化:
def getEntries(User user) {
def entries = [ClassName].createCriteria().list() {
calendar {
eq("user", user)
}
}
}
這並沒有引發異常,但也不能工作。
這裏是域類的相關部分:
class Calendar {
static belongsTo = [user: User]
static hasMany = [entries: Entries]
...
}
class User {
Calendar calendar
...
}
class Entry {
static belongsTo = [calendar: Calendar]
...
}
當谷歌搜索我遇到了類似的問題就來了早在2008年指出: http://jira.codehaus.org/browse/GRAILS-1412
但根據該鏈接這個問題應該已經解決很久以前。
我在做什麼錯?
您可以打開org.hibernate.SQL調試登錄電子,看看查詢它的運行?這可能有助於追蹤發生了什麼問題。 – leebutts
我試過,我得到的是: Q1:選擇this_.id作爲id8_0_,this_.version作爲版本8_0_,... 從用戶this_ where this_.username =? Q2:選擇top? (*)作爲y0_來自條目this_左 外部連接日曆calendar_a1_ on_calendar_id = calendar_a1_.id 其中((calendar_a1_.id =?)) Q3:select top? this_.id爲id3_1_,this_.version爲version3_1_,... 從入門THIS_左外連接日曆calendar_a1_上this_.calendar_id = calendar_a1_.id 其中((calendar_a1_.id =?)) 這似乎沒什麼問題。有任何想法嗎? (抱歉格式化,評論似乎沒有很好地對待代碼。) –
我沒有看到'where calender.user_id =?'的任何內容。雖然... – leebutts