hibernate - GORM Hibernate查询

标签 hibernate grails hql gorm

我有以下Grails域对象

class ProductType {
    String name
    static hasMany = [attributes: Attribute]
}

class Attribute {

    Boolean mandatory = false
    Integer seq

    static belongsTo = [productType: ProductType]
}

我想获取所有ProductType和它们的必需Attribute。此外,我希望选择的Attribute能够通过seq属性被急切地加载和排序。我已经尝试过各种HQL和Criteria查询,但似乎无法弄清楚。

最佳答案

此查询将返回所有具有强制属性的产品类型,这些属性会被紧急加载:

def types = ProductType.executeQuery("""
   select distinct type from ProductType type
   left join fetch type.attributes attribute
   where attribute.mandatory=true""")

这些属性位于映射集中,因此没有顺序,但是很容易收集和排序它们:
def sortedAttributes = types.collect { it.attributes }.flatten().sort { it.seq }

关于hibernate - GORM Hibernate查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1429848/

相关文章:

grails - 分页错误-Grails动态脚手架

Grails - Controller 链和模型消失

java - 从 HQL 中的时间戳提取日期以在 where 子句中进行比较

java - 使用外部 .sql 文件使用 hibernate session 创建数据库时出错

java - 使用 XMLslurper 获取内部数据

java - hql 去掉参数值的括号

java - hibernate 日期操作

NHibernate 属性公式过滤器

java - Spring + hibernate + JPA

Hibernate:一对多关系中集合的空索引列