Grails Projections不返回所有属性而不归类

时间:2012-12-17 09:41:09

标签: mysql json grails gorm createcriteria

如何获取它以便我返回下面的所有投影

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的”]}

我需要的地方:

{ “TOTALCOUNT”:2 “数据”:[{ “类”: “org.example.Company”, “ID”:1, “名称”: “公司1”},{ “类”:“有机.example.Company”, “ID”:2 “名称”: “Company2的”}]}

在公司域名中,我有很多关系(一对一,一对多......) 我的域名如下所示:

package org.example

import 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
}
}

任何帮助都会很棒:) ????

1 个答案:

答案 0 :(得分:0)

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

请参阅projection下的属性部分:'property返回返回结果中的给定属性'。我并没有按照“所有预测”得到你所要求的东西。

您只是想找到适合您所在域名的所有内容吗?你为什么要使用投影?

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

应该是

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

事实上,当我尝试按照你的方式进行操作时,我收到了编译错误。我仍然觉得简单地获取整个域本身更有意义,除非有一个非常具体的理由不这样做。

相关问题