1

次の Grails ドメイン オブジェクトがあります。

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

class Attribute {

    Boolean mandatory = false
    Integer seq

    static belongsTo = [productType: ProductType]
}

ProductTypeすべての とその必須のを取得したいと思いAttributeます。Attributeさらに、選択したs を積極的に読み込み、プロパティ別に並べ替えたいと思いseqます。あらゆる種類の HQL および Criteria クエリを試しましたが、理解できないようです。

4

2 に答える 2

1

または、多面ドメインにComparable Interfaceを実装し、片側に(デフォルトのセットの代わりに)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 に答える
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 に答える