0

我有一个看起来像这样的 Section 域。我从中构建导航,并在部分或子部分下排序故事,例如:新闻 > 犯罪 > 火车大劫案

class Section {
    String name;
    String path
    Section parent;
    Boolean active = true;
    int level = 0;
    Boolean isRoot = false;
    static hasMany = [children:Section]
    static mappedBy = [children:'parent']
    static mapping = {
        parent :'all-delete-orphan' 
        parent lazy:true
    }
}

看起来像这样的 Story 域

    class Story 
    {
        String title;
        String story;
        List link;
        List substory;
        List picture;
        Section publishUnder
        boolean parent = false;
        boolean approved = false;

        Date dateCreated;
        Date lastUpdated;

        static belongsTo = [author:Author]

        static hasMany=
        [
           link:String,
           substory:Story,
           picture:Picture
        ]
   }

我们有 7 个根部分:

家庭、新闻、体育、旅游、科技、文化和评论,除了家庭之外,它们都有子部分

例如:新闻将有一个包含英国、欧洲、美国的级别,然后美国可能有另一个级别的子部分,用于包含政治和犯罪部分。如上所述的故事存储在“部分”下。因此,“奥巴马被弹劾”的故事将在
新闻 > 美国 > 政治下发表。

我必须编写一个查询,该查询将获取 n 个故事,这些故事存储在顶级部分的一个子项下。因此,如果我将“新闻”部分作为参数传递(可能没有任何故事直接存储在“新闻”下,但可能在News > Uk> Crime下)我想返回存储在“消息”

我如何用gorm做到这一点?我应该使用 HQL 吗?我仍然是一个菜鸟,所以任何帮助将不胜感激。

4

1 回答 1

1

你可以有一个像这样的简单解决方案:

首先,为了加快速度,我会在内存缓存中创建一个 Sections Tree。当您启动应用程序时,您可以创建此树并将其存储在应用程序上下文中:在 grails-app/conf/BootStrap.grrovy 中,您可以在 init 闭包中执行此操作

def init = { servletContext ->
    servletContext.sectionsTree = something
}

要创建这个“东西”,您可以递归地遍历子部分并构造一个列表或映射,其中包含每个部分的所有子项的 id ......类似于:

rootSections = [[section:root1, childrenIds:[23,45,67,89,5,43,45,6,7,87], children:[section:child1, childrenIds:[23,45,67]]], [section:root2, childrenIds:[32,43,54,65,76], children:[]]]

其中 childrenIds 包含子项和子项的子项中的所有 ID。

所以现在,您可以执行一个简单的 HQL 查询来获取 Section´s Stories:

Story.executeQuery("select s from Story s where s.publishUnder.id in (?)", [childrenIds]);

那只是一种解决方案....看看你的想法。

干杯

于 2013-05-24T15:46:53.087 回答