Grails Projections not returning all properties and not grouped

孤者浪人 提交于 2019-12-07 12:44:59

问题


How to get it so i return all of the projections from the below

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

The result comes out like this:

{"totalCount":2,"data":["company1","company2"]}

Where i need it to be:

{"totalCount":2,"data":[{"class":"org.example.Company","id":1,"name":"company1"},{"class":"org.example.Company","id":2,"name":"company2"}]}

In the company domain i have lots of relationships (some one to one, one to many etc...) my domain looks like the following:

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

Any help would be great:) ????


回答1:


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

See the property section under projections: 'property Returns the given property in the returned results'. I don't really get what you are asking for by 'all the projections'.

Are you simply looking to Find all for your domain? Why are you using a projection?

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

should be

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

In fact, I get a compilation error when I attempt to do it your way. I still feel like it makes more sense to simply get the entire domain itself unless there is a very specific reason not to.



来源:https://stackoverflow.com/questions/13911615/grails-projections-not-returning-all-properties-and-not-grouped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!