如何让 Hibernate FetchProfile 加载层次结构中的深层子对象

发布于 2024-10-26 09:54:57 字数 871 浏览 2 评论 0原文

我和 Hibernate 社区中的某人有同样的问题:FetchProfiles

出于性能原因,我在数据模型中有如下关系:

...C -[FetchType.LAZY]-> D -> [FetchType.LAZY] -> E

使用 FetchProfile 我可以用 C 急切地加载 D,但我不知道如何急切地加载 E。我知道我可以使用内部联接成功使用 NamedQuery,但是我不知道如何使用 FetchProfile 来做到这一点,这真的让我很烦恼。尝试 FetchProfile 的示例(任何其他内容都会在时间的迷雾中丢失):

@FetchProfile(name = "cwithDAndE", fetchOverrides = {
        @FetchProfile.FetchOverride(entity = C.class, association = "dByCId", mode = FetchMode.JOIN),
        @FetchProfile.FetchOverride(entity = D.class, association = "eByDId", mode = FetchMode.JOIN)
})

我为会话启用 FetchProfile 并成功使用 session.get,没有错误,并且 C 和 D 已填充 - E 仍然是惰性的且未填充。在绝望中,我记得尝试用点符号表示从 C 向下的关联。我只能找到深度为 1 的示例。

这是我知识中的一个强迫症类型的空白,需要填补!

预先感谢您的任何帮助。

I have the same question as someone posted in the Hibernate Community: FetchProfiles.

For performance reasons I have a relationship in the data model as follows:

...C -[FetchType.LAZY]-> D -> [FetchType.LAZY] -> E

Using FetchProfile I can eagerly load D with C, but I can't figure out how to eagerly load E. I know I can successfully use a NamedQuery using inner joins, but it really bugs me that I can't work out how to do it using FetchProfile. An example of an attempted FetchProfile (anything else is lost in the mists of time):

@FetchProfile(name = "cwithDAndE", fetchOverrides = {
        @FetchProfile.FetchOverride(entity = C.class, association = "dByCId", mode = FetchMode.JOIN),
        @FetchProfile.FetchOverride(entity = D.class, association = "eByDId", mode = FetchMode.JOIN)
})

I enable the FetchProfile for the session and successfully use a session.get with no error and C and D populated - E is still lazy and unpopulated. In desperation I remember trying a dot notation for the association from C downwards. I can only find examples that have a depth of one.

This is an OCD type gap in my knowledge that needs filling!

Thanks in advance for any help.

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

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

发布评论

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

评论(1

沙沙粒小 2024-11-02 09:54:57

你有一个 A 对象,其中包含一个 B 对象,又包含一个 C 对象。默认情况下,它们是

...A -[FetchType.LAZY]-> B -> [FetchType.LAZY] -> C

A类:

@FetchProfiles({
    @FetchProfile(fetchOverrides = { @FetchOverride(association = "b", entity = A.class, mode = FetchMode.JOIN) }, name = "a-with-b")
    })
@Entity
@Table(name="EDITOR_IDENTITIES")
public class A {
    private B b;    
    //...
}

B类:

@FetchProfiles({
    @FetchProfile(fetchOverrides = { @FetchOverride(association = "c", entity = B.class, mode = FetchMode.JOIN) }, name = "b-with-c")
    })
@Entity
@Table(name="EDITOR_IDENTITIES")
public class B {
    private C c;    
    //...
}

ADao类:

@Repository(value="aDaoImpl")
@Transactional
public class ADaoImpl {

    @Override
    public A loadByPrimaryKey(long id)
    {   
        Session session = sessionFactory.getCurrentSession();
        session.enableFetchProfile("a-with-b");
        session.enableFetchProfile("b-with-c");
        Criteria criteria = session.createCriteria(A.class);
        criteria.add(Restrictions.eq("id", id));
        A a = (A) criteria.uniqueResult();
        if(identity != null)
            return identity;
        else
            return null;
    }
}

你会得到A填充B填充C。这是一个非常基本的解决方案,您可以构建一个 Dao 方法,以获取配置文件列表作为参数。

You have a A obj containing a B obj, containing a C obj. By default they are

...A -[FetchType.LAZY]-> B -> [FetchType.LAZY] -> C

A class:

@FetchProfiles({
    @FetchProfile(fetchOverrides = { @FetchOverride(association = "b", entity = A.class, mode = FetchMode.JOIN) }, name = "a-with-b")
    })
@Entity
@Table(name="EDITOR_IDENTITIES")
public class A {
    private B b;    
    //...
}

B class:

@FetchProfiles({
    @FetchProfile(fetchOverrides = { @FetchOverride(association = "c", entity = B.class, mode = FetchMode.JOIN) }, name = "b-with-c")
    })
@Entity
@Table(name="EDITOR_IDENTITIES")
public class B {
    private C c;    
    //...
}

ADao class:

@Repository(value="aDaoImpl")
@Transactional
public class ADaoImpl {

    @Override
    public A loadByPrimaryKey(long id)
    {   
        Session session = sessionFactory.getCurrentSession();
        session.enableFetchProfile("a-with-b");
        session.enableFetchProfile("b-with-c");
        Criteria criteria = session.createCriteria(A.class);
        criteria.add(Restrictions.eq("id", id));
        A a = (A) criteria.uniqueResult();
        if(identity != null)
            return identity;
        else
            return null;
    }
}

you will get A filled with B filled with C. This is a very basic solution, you can build a Dao method taking a list of fetch profile as argument.

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