【问题标题】:Grails Projections not returning all properties and not groupedGrails Projections 未返回所有属性且未分组
【发布时间】:2023-04-07 04:13:01
【问题描述】:

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

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.Company","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
}
}

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

【问题讨论】:

标签: mysql json grails grails-orm createcriteria


【解决方案1】:

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

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

【讨论】:

  • 我想同时投影 id 和 name ,但由于某种原因,当我测试函数时只有名称被投影....
  • 嗨 joseph 感谢您的帮助...这仍然没有任何作用 - 我不知道为什么?我只想返回 id 和 name,而不是我域中的其他所有内容(因此是预测)。该域有很多项目,所以如果我根本不使用投影......它会返回所有内容(巨大的文件),当我只是投影 id,name 正如你在上面显示的那样,我在原始问题中遇到了问题。 ..
  • 我会将 SQL 日志记录添加到您的代码中,以查看查询打印的内容。 stackoverflow.com/questions/2568507/…
猜你喜欢
  • 2015-05-22
  • 2021-09-21
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2021-09-23
  • 2023-03-23
相关资源
最近更新 更多