Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails查询:获取关联对象的列表_Grails_Gorm_Hibernate Criteria - Fatal编程技术网

Grails查询:获取关联对象的列表

Grails查询:获取关联对象的列表,grails,gorm,hibernate-criteria,Grails,Gorm,Hibernate Criteria,我有一种多对多的关系: class Project { Set<PrincipalInvestigator> pis : static hasMany = [ pis:PrincipalInvestigator ] } class PrincipalInvestigator { String name : } 我的解决方案快了一个数量级,但仍然是两个不同的查询。有没有一种方法可以在一个查询中获得相同的结果?使用executeQuery

我有一种多对多的关系:

class Project {
    Set<PrincipalInvestigator> pis
     :

    static hasMany = [ pis:PrincipalInvestigator ]
}

class PrincipalInvestigator {
    String name
     :
}
我的解决方案快了一个数量级,但仍然是两个不同的查询。有没有一种方法可以在一个查询中获得相同的结果?

使用executeQuery可以使查询变得更简单。应该采取以下措施:

PrincipalInvestigator.executeQuery("select distinct p.pis from Project p where p.id in :projectIds", 
    [projectIds: [1,2,3]])

我的问题的解决方案是以下HQL:

PrincipalInvestigator.executeQuery(
  "select distinct pi from Project p inner join p.pis as pi where p.id in :projectIds order by pi.name",
  [projectIds:[1,2,3]])

此解决方案允许对不同的结果进行排序,内部联接将修剪所有空实例。感谢cfrick让我走上正轨

多谢各位。您将如何使PI列表唯一化。选择“不同”并正确排序?
PrincipalInvestigator.executeQuery(
  "select distinct pi from Project p inner join p.pis as pi where p.id in :projectIds order by pi.name",
  [projectIds:[1,2,3]])