GORM Hibernate查询
我有以下Grails域对象GORM Hibernate查询,hibernate,grails,hql,gorm,Hibernate,Grails,Hql,Gorm,我有以下Grails域对象 class ProductType { String name static hasMany = [attributes: Attribute] } class Attribute { Boolean mandatory = false Integer seq static belongsTo = [productType: ProductType] } 我想获取所有ProductTypes及其必需的属性。此外,我希望所
class ProductType {
String name
static hasMany = [attributes: Attribute]
}
class Attribute {
Boolean mandatory = false
Integer seq
static belongsTo = [productType: ProductType]
}
我想获取所有
ProductType
s及其必需的属性。此外,我希望所选的属性
s能够按照seq
属性进行加载和排序。我尝试了各种HQL和Criteria查询,但似乎都没有找到答案。此查询将返回所有具有强制属性的ProductType,并急切地加载这些属性:
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 }
或者,您可以通过在多方域中实现一个可比较的接口,并在一侧(而不是默认集)中注入SortedSet,来获得多方的排序列表
class ProductType {
String name
SortedSet attributes
static hasMany = [attributes: Attribute]
}
class Attribute implements Comparable {
Boolean mandatory = false
Integer seq
static belongsTo = [productType: ProductType]
int compareTo(obj) {
seq.compareTo(obj.seq)
}
}