问题描述
我在我的项目中使用spring + hibernate;我有两个类提醒
和客户端
在课堂提醒中,我添加了多对一的关系客户端,它是默认情况下急切加载。我想要这个对象图在我的项目中的大多数情况下,所以我已经设置提醒类中的客户提取类型
类提醒{
@ManyToOne
客户端客户端;
$ / code>
但是对于一个或两个场景我想保留这个对象客户端的初始化懒惰;
因此我在提取提示的方法中添加了如下所示:
标准c = session.createCriteria();
c.setFetchMode(client,FetchMode.SELECT);
hibernateTemplate.findByCriteria(criteria);
它不工作;它仍然加载提示客户对象
而反向(从懒惰到渴望)工作正常
解决方案从api文档:
AFAIK,如果映射是标记为懒惰,您可以使用标准或HQL查询热切地获取,但是不能做到相反:如果映射被标记为渴望,那么它总是会被热切地获取。
I am using spring + hibernate in my project; I have two classes Reminder
and Client
in class reminder i have added a relationship of many to one for client and it is by default eagerly loaded. I want this Object graph most of the scenarios in my project so i have set fetch type eager for client in reminder class
Class Reminder {
@ManyToOne
Client client;
}
but for one or two scenarios i want to keep initialization of this object client lazy;
so i have added in method for fetching reminders is like below
Criteria c = session.createCriteria();
c.setFetchMode("client", FetchMode.SELECT);
hibernateTemplate.findByCriteria(criteria);
it is not working; it still loads client objects with reminder
while reverse (from lazy to eager) is working fine
解决方案 From the api doc:
AFAIK, if a mapping is marked as lazy, you may fetch eagerly using a criteria or HQL query, but you can't do the reverse : if a mapping is marked as eager, then it'll always be fetched eagerly.
这篇关于对象的动态延迟加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!