سؤال

ولدي كائنات المجال الكؤوس المقدسة التالي

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

class Attribute {

    Boolean mandatory = false
    Integer seq

    static belongsTo = [productType: ProductType]
}

وأود أن تحصل على كل ProductTypes وAttributes على إلزامية. وعلاوة على ذلك، أود أن Attributes المحدد إلى تحميلها بفارغ الصبر وفرزها بواسطة الخاصية seq. لقد جربت كل أنواع HQL ومعايير الاستفسارات، ولكن لا يمكن أن يبدو لأنها من أصل الرقم.

هل كانت مفيدة؟

المحلول

وهذا الاستعلام سيعود كل 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 }

نصائح أخرى

وبدلا من ذلك، يمكنك الحصول على قائمة مفروزة من جانب العديد من خلال تنفيذ واجهة قابلة للمقارنة في مجال أطراف جانب، وحقن 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)
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top