【问题标题】:How to use "Grouped BY" with Grails criteria among three tables如何在三个表中使用“Grouped BY”和 Grails 标准
【发布时间】:2013-04-27 10:17:40
【问题描述】:

如果我有三个这样的域类:

class Candidate  {
    static belongsto=[application:Application]

}


class Vote {
   String name;
static belongsto=[application:Application]

}

class Application {


    Date datedemand;
   Candidate candidates; 
     Vote votes
    static hasMany = [candidates:Candidate,votes:Vote]

}

我想检索按投票名称分组的所有候选人。

我开始了以下尝试,但我仍然被阻止:

def criteria =  Application.createCriteria()
def results = criteria.list { 
    votes {

     }
    candidates{


    }

}

【问题讨论】:

  • 谢谢。但是,它没有用。

标签: grails criteria jointable


【解决方案1】:

在像样的 grails (2.x+) 中,您可以尝试类似的东西:

Application.withCriteria {
   createAlias("votes", votes)
   projections {
      groupProperty("votes.name")
   }
}

【讨论】:

  • 我不认为这是有效的。首先,votes.name 无效。 namevote 而不是 votes 的属性。其次,他需要“按投票姓名分组的所有候选人”。
【解决方案2】:

在这种情况下,我会选择一个简单的普通 HQL 而不是 Criteria,类似这样或更多(根据需要使用 join):

String hql = "Select V.name, C.name " + 
             "from Candidate C, Vote V " + 
             "where C.application = V.application " +
             "group by V.name, C.name"

Query query = session.createQuery(hql)
List results = query.list()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多