开发者

HQL / Nested Eager Loading

I am wondering what the best way is to load nested values for lazy loaded objects. I'm providing an example to help explain this better.

public class A{
    private B b; //Lazy loaded
    private C c; //Lazy loaded
    private D d; //Lazy loaded
}
public class B{
    private E e; //Lazy loaded
    private F f; //Lazy loaded
}
public class C{
}
public class D{
}

As an example I want to do:

System.out.println(a.getB().getE());

If I ran the开发者_Python百科 above statement I'd get a lazy load exception.

I can always do the following:

for (A a : somePossiblyLargeList) {
    org.hibernate.Hibernate.initialize(a.getB().getE());
}

but obviously performance would suck.

Is there a way I can write a custom HQL query which returns A objects that are pre-populated with those specific nested fields?

Thanks!


Of course.

Use join fetch in your HQL query, as explained in the Hibernate reference documentation (that you should read):

select a from A a left join fetch a.b b left join fetch b.e e where ...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜