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]])