【问题标题】:GORM - Query one to many association in GrailsGORM - 在 Grails 中查询一对多关联
【发布时间】:2013-03-23 08:05:39
【问题描述】:

我有课程域

class Course {
    String name

    static hasMany = [categories: Category]
}

分类领域类

class Category {
    String name 
}

所以这里一个课程可以有多个类别。

现在我想查找所有具有 ID 为 4 的类别的课程

我尝试编写 HQL 查询:

def courseList = Course.findAll("from Course as c where c.categories.id in (4)")

给出一个错误。

如何编写正确的 HQL 或正确的 withCriteria 查询?

【问题讨论】:

  • 这是一个错字还是你真的想在同一侧使用 hasMany 和 belongsTo?只是看起来不太对。如果一门课程有很多类别,那么它就不能属于一个类别。
  • @uchamp: 这是类型错误,更新了我的问题..

标签: hibernate grails hql grails-orm criteria


【解决方案1】:

您可以使用 withCriteria 查询:

Course.withCriteria { 
    categories { 
        eq 'id', new Long(4)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多