3

如何得到它,所以我从下面返回所有的预测

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,"data":["company1","company2"]}

我需要它在哪里:

{"totalCount":2,"data":[{"class":"org.example.Company","id":1,"name":"company1"},{"class":"org.example.公司","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
}
}

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

4

1 回答 1

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

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

于 2012-12-17T22:01:16.360 回答