对象的动态延迟加载不起作用 [英] dynamically lazy loading of objects does not work

查看:104
本文介绍了对象的动态延迟加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用spring + hibernate;我有两个类提醒客户端
在课堂提醒中,我添加了多对一的关系客户端,它是默认情况下急切加载。我想要这个对象图在我的项目中的大多数情况下,所以我已经设置提醒类中的客户提取类型

 类提醒{
@ManyToOne
客户端客户端;

$ / code>

但是对于一个或两个场景我想保留这个对象客户端的初始化懒惰;

因此我在提取提示的方法中添加了如下所示:

 标准c = session.createCriteria(); 
c.setFetchMode(client,FetchMode.SELECT);
hibernateTemplate.findByCriteria(criteria);

它不工作;它仍然加载提示客户对象



而反向(从懒惰到渴望)工作正常

解决方案

从api文档:


public static final FetchMode SELECT

 使用单独的选择快速获取。等同于fetch =select


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:

public static final FetchMode SELECT

Fetch eagerly, using a separate select. Equivalent to fetch="select"

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.

这篇关于对象的动态延迟加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆