Hibernate 在Grails中查询与枚举的关联,其中查询

Hibernate 在Grails中查询与枚举的关联,其中查询,hibernate,grails,gorm,Hibernate,Grails,Gorm,我对Grails 3.2.6有一个查询问题,我创建了一个示例项目来重现这个问题: 在这个项目中,我有3个实体:项目有许多TODO,而这些TODO又有许多TODO。TODO也可以直接关联到项目,而无需通过TodoList。因此,Todo要么有父项目,要么有父TodoList,但决不能同时有两个 现在在我的控制器中,我想直接或间接地加载与给定类型的项目关联的所有TODO,其中类型是枚举 以下是控制器操作的代码: def index(Integer max, String projectType) {

我对Grails 3.2.6有一个查询问题,我创建了一个示例项目来重现这个问题:

在这个项目中,我有3个实体:项目有许多TODO,而这些TODO又有许多TODO。TODO也可以直接关联到项目,而无需通过TodoList。因此,Todo要么有父项目,要么有父TodoList,但决不能同时有两个

现在在我的控制器中,我想直接或间接地加载与给定类型的项目关联的所有TODO,其中类型是枚举

以下是控制器操作的代码:

def index(Integer max, String projectType) {
    def type = projectType ? ProjectType.valueOf(projectType) : null
    params.max = Math.min(max ?: 10, 100)
    if(type) {
        def todoQuery = Todo.where {
            parentProject.type == type || parentList.parentProject.type == type
        }
        respond todoQuery.list(params), model: [todoCount: todoQuery.count()]
    } else {
        respond Todo.list(params), model:[todoCount: Todo.count()]
    }
}
如果我没有指定类型,它将正确返回我在引导中创建的所有实例。但如果在查询中指定项目类型,结果列表将为空

我的询问似乎有问题,但我不知道是什么问题

请注意,我在Criteria查询中看到过使用指定联接之类的示例,但是我不知道如何对计数重用相同的查询

编辑:以下是当我得到
http://localhost:8080/todo?projectType=ENGINEERING

select count(*) as y0_ from todo this_ inner join todo_list parentlist2_ on this_.parent_list_id=parentlist2_.id inner join project parentproj1_ on this_.parent_project_id=parentproj1_.id where ((parentproj1_.type=?) or ((parentproj1_.type=?))) limit ?
select this_.id as id1_1_2_, this_.version as version2_1_2_, this_.parent_list_id as parent_l3_1_2_, this_.parent_project_id as parent_p4_1_2_, this_.task as task5_1_2_, parentlist2_.id as id1_2_0_, parentlist2_.version as version2_2_0_, parentlist2_.parent_project_id as parent_p3_2_0_, parentlist2_.title as title4_2_0_, parentproj1_.id as id1_0_1_, parentproj1_.version as version2_0_1_, parentproj1_.name as name3_0_1_, parentproj1_.type as type4_0_1_ from todo this_ inner join todo_list parentlist2_ on this_.parent_list_id=parentlist2_.id inner join project parentproj1_ on this_.parent_project_id=parentproj1_.id where ((parentproj1_.type=?) or ((parentproj1_.type=?))) limit ?

您的枚举没有值键
ProjectType.${ProjectType}“
vs
ProjectType.valueOf(ProjectType)
这会改变行为吗?记录sql并inspect@vahid通过断点,我可以检查类型变量是否使用正确的枚举常量正确初始化。valueOf()正确地将字符串转换为相应的枚举常量。您在模糊实际问题时遇到了一个问题:当您只执行
where{list.project.type==type}
时,它将返回所有行。如果您在todo中使用其他内容重命名列表,它将正确地使用where进行过滤。至于您的实际问题,您的todo 1、2、3链接到列表,而4、5链接到项目。当您执行where时,它会在两个表上创建一个内部联接,但它们是互斥的。解决方法通常是使用外部联接,但我不知道如何在GORM Where子句中这样做。枚举没有值键
ProjectType。“${ProjectType}”
vs
ProjectType.valueOf(ProjectType)
这会改变行为吗?记录sql和inspect@vahid有一个断点,我可以检查类型变量是否使用正确的枚举常量正确初始化。valueOf()正确地将字符串转换为相应的枚举常量。您在模糊实际问题时遇到了一个问题:当您只执行
where{list.project.type==type}
时,它将返回所有行。如果您在todo中使用其他内容重命名列表,它将正确地使用where进行过滤。至于您的实际问题,您的todo 1、2、3链接到列表,而4、5链接到项目。当您执行where时,它会在两个表上创建一个内部联接,但它们是互斥的。解决这个问题的方法通常是使用外部连接,但我不知道如何在GORM Where子句中这样做。