开发者

How to get Hibernate FetchProfile to load deep child objects in the hierarchy

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜