2013-10-17 57 views
0

我在Grails中有以下域模型我試圖運行一個查詢,但它失敗,說這是一個無效的查詢。爲什麼這個Grails(GORM)查詢無效?

查詢看起來像:

StringBuilder queryBuf = new StringBuilder(); 
    queryBuf.append("select rr.role from ReportRole rr "); 
    def newroles = ReportRole.findAll(queryBuf) 

而且,域看起來是這樣:

package auth 

import java.util.Date 
import auth.Report 
import auth.Role 

class ReportRole { 
Long id 
Report report 
Role role 
Date dateCreated 
Date lastUpdated 
Person createdBy 

static mapping = { 
    table 'CIT_RM_Report_Role' 
    version false 
    role joinTable:[name:'AU_ROLE_DESCR', key:'role_id', column:'id'] 
    columns { 
     id column:'report_role_id' 
     report column:'report_id' 
     createdBy column:'created_by' 
     dateCreated column:'create_date' 
     lastUpdated column:'last_updated' 

     } 
    } 
} 

package auth; 
class Role { 
static hasMany = [people: Person] 
Long id; 
String authority; 
String description; 
static mapping = { 
    table 'AU_ROLE_DESCR' 
    people joinTable:[name:'AU_PERSON_ROLE', key:'AUTHORITY_ID', column:'PERSON_ID'] 
    version false; 
    } 
} 

誰能告訴我這是爲什麼無效。我有一些類似這樣的查詢將工作的類似的域。

+0

精確的錯誤是:無效的查詢[從ReportRole RR選擇rr.role]域類[類auth.ReportRole] –

+0

我想我可能已經有鑰匙/列混在一起,所以我試着扭轉它們,但同樣的錯誤。 –

回答

2

我認爲findAll是限制回來與域名單,而不是特別是任何關聯/元素。您可以更好地利用executeQuery達到你所追求的:

ReportRole.executeQuery("select rr.role from ReportRole rr") 
+0

謝謝你,這個伎倆。 –