1

我有以下 Grails 域对象

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

class Attribute {

    Boolean mandatory = false
    Integer seq

    static belongsTo = [productType: ProductType]
}

我想获得所有ProductTypes 和他们的强制Attributes。此外,我希望被选中Attribute的 s 被seq属性急切地加载和排序。我尝试了各种 HQL 和 Criteria 查询,但似乎无法弄清楚。

4

2 回答 2

1

此查询将返回所有具有强制属性的 ProductTypes,并热切加载这些属性:

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 }
于 2009-09-15T23:50:03.580 回答
1

或者,您可以通过在多方域中实现一个 Comparable 接口并在一侧(而不是默认的 Set)中注入 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)
    }
}
于 2009-09-19T04:15:08.100 回答