2012-12-17 66 views
2

如何獲得它,所以我返回所有的預測從下面Grails的預測沒有返回的所有屬性,而不是分組

def c = Company.createCriteria() 
def a = c.list(params){ 
    projections{ 
     property 'id', property 'name' 
    } 
} 

if(a.size() == 0) 
    render "404" 
else { 
    render (contentType: 'text/json'){ 
      totalCount = a.totalCount 
      data = a 
    } 
    } 

結果出來是這樣的:

{「TOTALCOUNT」:2 「數據」:[ 「公司1」, 「Company2的」]}

其中i需要它是:

{ 「TOTALCOUNT」:2 「數據」:[{ 「類」:「有機example.Company 「 」ID「:1, 」名「: 」公司1「},{ 」類「:」 org.example.Com公司域名「,」id「:2,」name「:」company2「}]}

在公司域名中我有很多關係(一對一,一對多等) 我的域名外觀像下面這樣:

包org.example

進口的java.sql.Timestamp

class Company { 

String name 
String abn 
String cname 
String email 
String phone 
String position 
String address 
String city 
String postcode 
int style 
int openbookings; 

Date date; 

int tokenTotal = 0 

int totaltokens 
int totalboosts 
int totalposts 
Timestamp tokenstamp 

static hasMany = [users: User, broadcast: Broadcast, bookings: Booking, locations: Location,vimsurvey:VimSurvey,rewards: Reward, tokens: CompanyToken] 


static constraints = { 
    abn nullable: true 
    date nullable: true 
    style nullable: true 
} 
} 

任何幫助將是巨大的:) ????

+0

有沒有找到答案?我有一個類似的問題在這裏http://stackoverflow.com/questions/15250974/createcriteria-with-projections-does-not-select-all-columns – Sap

回答

0

http://grails.org/doc/1.1/ref/Domain%20Classes/createCriteria.html

參見下預測地產板塊:「屬性在返回的結果返回給定屬性」。我沒有真正明白你所要求的'所有的預測'。

你只是想找到你所有的域名?你爲什麼使用投影?

def a = c.list(params){ 
projections{ 
    property 'id', property 'name' 
} 
} 

應該

def a = c.list(params){ 
projections{ 
    property 'id' 
    property 'name' 
} 
} 

事實上,我得到一個編譯錯誤,當我嘗試做你的方式。我仍然覺得,除非有一個非特定的原因,否則簡單地獲取整個域本身就更有意義。

+0

我想同時項目ID和名稱,但由於某種原因只有名稱正在投影,當我測試功能.... –

+0

我編輯了我的答案。 – Joseph

+0

嗨約瑟夫感謝您的幫助...這仍然沒有做任何事情 - 我不知道爲什麼?我只想返回id和name,而不是我域中的所有其他內容(因此投影)。該領域有許多項目,所以如果我不使用投影...它會返回一切(巨大的文件),當我只是項目的ID,名稱,你給我上面,我在我原來的問題得到的問題。 .. –

相關問題