2012-04-19 66 views
2

以下是我的两个班Grails的预测由集团和计​​数

class Users { 
    String emailAddress 
    String password 
    // String filename 
    String firstName 
    String lastName 
    Date dateCreated 
    Date lastUpdated 
} 

class SharedDocuments { 
    Users author 
    Users receiver 
    Documents file 
    static constraints = { 

    } 
} 

我想运行与此类似的查询,基本上是我想要得到所有用户的列表随着文件数量以及他们所撰写

SELECT author_id, COUNT(SharedDocuments.id)FROM SharedDocuments 
    INNER JOIN users ON author_id = users.id 
    GROUP BY author_id 

这是我迄今为止

def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){ 
    createAlias("author","a") 
    eq("receiver.id",session.uid) 
    projections{ 
     groupProperty "author" 
     count "id",'mycount' 

    } 
    order('mycount','desc') 
    maxResults(params.max) 
} 

我有这个90%的工作,如果我摆脱countcountDistinct我得到不同的作者名单,但我想要的是与文档的计数一起作者。所以当我添加count或countDistinct子句到这个标准,我只是得到数组长! 像[2,3,4]我要的是[author1,2],[author2,3] ...] 我怎样才能实现这一目标我已经看到Grails: Projection on many tables?Grails criteria projections - get rows countGrails groupProperty and order. How it works?, 但没有的答案似乎解决了我的问题!

回答

0

你不能只是使用countBy * - 像

SharedDocuments.countByAuthorAndReceiver(user, receiver) 
0

如果你想要的结果,你在查询中所描述,那么你需要作者或任何其他特定的属性,如电子邮件的ID。


def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){ 
    eq("receiver.id",session.uid) 
    projections{ 
     groupProperty "author" 
     count "id",'mycount' 

    } 
    order('mycount','desc') 
    maxResults(params.max) 
} 
+0

无论我做什么我的结果集只是没有其他信息计数。在你给我的代码中打印sharedDocumentsInstanceList生成'[2,]'输出 – Sap 2012-04-19 14:49:20

+0

我做了一些完全一样的事情,但用命名查询,没有maxResults,它对我来说工作正常:(。顺便说一句,我甚至没有做author.id只是分组在作者身上也应该工作 – 2012-04-21 05:38:30