0

次のドメイン クラスがあります。

class ParentClass {
    String myProperty
    static hasMany = [ childClasses : ChildClass]
}

class ChildClass {
    Date birthday
    static belongsTo = [ parentClass : ParentClass]
}

最近の子の生年月日順に ParentClasses を一覧表示できるようにする必要があります。params通常の CRUD リスト ページで使用されているを使用できれば最高です。

この特定の順序付けのためにコントローラーに特定のルールを記述できることはわかっていますが、それは最善の解決策ではなく、基準に基づいて派生プロパティまたは一時プロパティの線に沿って何かを使用する必要があるように感じますが、できませんでしたそれを行う方法が見つかりません(私はGrailsとHibernateに慣れていません)。

任意のポインタをいただければ幸いです。

編集:私はこれを行うことができました:

static mapping = {
    mostRecentBirthday formula: '(
        SELECT c.data
        FROM ChildClass c
        WHERE c.parent_class_id=id
        ORDER BY c.birthday DESC LIMIT 1
    )'
}

このソリューションで私が抱えている唯一の問題はparent_class_id、そこにハードコードされていることです。これをもっと正確に書く方法はありますか?

4

2 に答える 2

0

私が正しく理解していれば、次のようにすることができます:

def parentClass = new ParentClass(params.id);
// let this be your parentclass 
// you want to have the childs from ordered by birthday

def childListOrderedByBirthday = ChildClass.withCriteria {
    parentClass {
        eq("id", parentClass.id);
    }
    order("birthday", "desc")
}.list()
于 2011-08-20T01:06:31.130 に答える
0

An alternate to crudolf's approach is to use a dynamic finder. It would look like this:

def parentClass = new ParentClass(params.id);

def childListOrderedByBirthday =
        ChildClass.findAllByParentClass(parentClass,
                [sort: "birthday", order: "desc"]);

This may be cleaner to read.

于 2011-08-20T03:54:21.757 に答える