我有以下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/