HQL / 嵌套预加载
我想知道加载延迟加载对象的嵌套值的最佳方法是什么。我提供一个例子来帮助更好地解释这一点。
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{
}
作为一个例子,我想做:
System.out.println(a.getB().getE());
如果我运行上面的语句,我会得到一个延迟加载异常。
我总是可以执行以下操作:
for (A a : somePossiblyLargeList) {
org.hibernate.Hibernate.initialize(a.getB().getE());
}
但显然性能会很糟糕。
有没有一种方法可以编写自定义 HQL 查询,该查询返回预先填充了这些特定嵌套字段的 A 对象?
谢谢!
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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然。
在 HQL 查询中使用
join fetch
,如 Hibernate 参考文档(您应该阅读):Of course.
Use
join fetch
in your HQL query, as explained in the Hibernate reference documentation (that you should read):