Nhibernate 一对多场景下的智能预取

发布于 2025-01-06 03:46:13 字数 866 浏览 0 评论 0原文

我们使用带有流畅映射的 NHibernate 3.2。以下是高度可扩展的性能敏感系统中的简单一对多映射。

    public class Root
    {
        public Root()
        {
            Childs = new List<Child>();
        }
        public virtual int ID { get; set; }
        public virtual IList<Child> Childs { get; set; }
        public virtual int LastChildID { get; set; }
        public virtual int CurrentChildID { get; set; }
    }

    public class Child
    {
        public virtual int ID { get; set; }
        public virtual Root Root { get; set; }
        public virtual string Name { get; set; }
    }

当我们在几乎所有情况下从存储库加载此映射时,我们只对 CurrentChildID 定义的 Current Child 感兴趣,并且希望从数据库中一次性读取此内容以及根记录,但没有其他子记录。如果我们对剩余的子项感兴趣,我们只需要加载 LastChildID 和 CurrentChildID 之间的记录 - 原因是我们可能有数百个子项作为根,并且 LastChildID 之前的所有内容都将被标记为存档/数据仓库,并进行处理分别。

我们如何在 NHibernate 中配置预取。我们只是找不到例子。

We are using NHibernate 3.2 with fluent mappings.The following is a simple 1 to Many mapping in highly scalable performance sensitive system

    public class Root
    {
        public Root()
        {
            Childs = new List<Child>();
        }
        public virtual int ID { get; set; }
        public virtual IList<Child> Childs { get; set; }
        public virtual int LastChildID { get; set; }
        public virtual int CurrentChildID { get; set; }
    }

    public class Child
    {
        public virtual int ID { get; set; }
        public virtual Root Root { get; set; }
        public virtual string Name { get; set; }
    }

When we load this from the repository in almost all cases we are only interest in the Current Child as defined by CurrentChildID and would want to read this in one hit from the database along with the root but no other Child records. If we are then interested in the remaining children we only need to load the records between LastChildID and CurrentChildID - the reason for this is we may have hundreds of children for the root and everything before the LastChildID will be marked for archiving/datawarehousing which is handled seperately.

How do we configure the pre-fetching in NHibernate. WE just cannot find an example.

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

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

发布评论

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

评论(1

傲鸠 2025-01-13 03:46:13

我将更改类并实现 getter 和 setter,以使它们在所有三个属性中添加和删除

public class Root
{
    public Root()
    {
        Childs = new List<Child>();
        LastChilds = new List<Child>();
    }

    public virtual int ID { get; set; }
    public virtual IList<Child> AllChilds { get; private set; }
    public virtual IList<Child> ActiveChilds { get; private set; }
    public virtual Child CurrentChild { get; set; }
}

一致

public class RootMap : ClassMap<Root>
{
    public RootMap()
    {

        HasMany(r => r.AllChilds);

        HasMany(r => r.ActiveChilds)
            .Where("archived = false"); // the childs know if they are archived otherwise it will get more complex

        References(r => r.CurrentChild, "LastChildID")
            .Not.LazyLoad();
    }
}

i would change the class and implement getter and setters to make them consistent adding and removing from all three properties

public class Root
{
    public Root()
    {
        Childs = new List<Child>();
        LastChilds = new List<Child>();
    }

    public virtual int ID { get; set; }
    public virtual IList<Child> AllChilds { get; private set; }
    public virtual IList<Child> ActiveChilds { get; private set; }
    public virtual Child CurrentChild { get; set; }
}

then

public class RootMap : ClassMap<Root>
{
    public RootMap()
    {

        HasMany(r => r.AllChilds);

        HasMany(r => r.ActiveChilds)
            .Where("archived = false"); // the childs know if they are archived otherwise it will get more complex

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