如何基于 Grails 上的子集合属性创建域瞬态属性?

发布于 2024-11-30 15:09:53 字数 779 浏览 0 评论 0原文

我有以下域类:

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

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

我需要能够列出按最近孩子的出生日期排序的 ParentClass。如果我能够使用常规 CRUD 列表页面中使用的 params 那就最好了。

我知道我可以在控制器中为这个特定的顺序编写一个特定的规则,但我觉得这不是最好的解决方案,我应该根据标准使用派生属性或瞬态属性的一些东西,但不能找不到方法来做到这一点(我对 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 是硬编码在那里的。有更正确的写法吗?

I have the following domain classes:

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

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

I need to be able to list ParentClasses ordered by the birthdate of their most recent child. It would be best if I were able to use the params used in the regular CRUD list pages.

I know I could write a specific rule in the controller for this specific ordering, but I feel like it is not the best solution and that I should use something along the lines of a derived property or a transient property based on a criteria, but couldn't find a way to do it (I'm kind of new to Grails and Hibernate).

Any pointers would be appreciated.

Edit: I managed to do this:

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

The only problem I have with this solution is that parent_class_id is hard coded in there. Is there a more correct way to write this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2024-12-07 15:09:53

如果我理解正确的话,你可以这样做:

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()

If I understand correctly, then you can do it like this:

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()
瞎闹 2024-12-07 15:09:53

crudolf 方法的替代方法是使用 动态查找器。它看起来像这样:

def parentClass = new ParentClass(params.id);

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

这可能更容易阅读。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文