哪个Hibernate FetchMode会导致集合被延迟加载? [英] Which Hibernate FetchMode will cause collections to be lazy-loaded?

查看:114
本文介绍了哪个Hibernate FetchMode会导致集合被延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate论坛上提出了这个问题,但没有得到完整的答复,所以我想我会在这里重新发布它。以下是原始问题主题:



http://forum.hibernate.org/viewtopic.php?f=1&t=1008243&p=2438963#p2438963



简而言之,我有一个自定义HQL查询,它将两个完全无关的表连接在一起,我试图通过禁用加载关联集合来防止O(N + 1)选择问题存在于我的Hibernate映射的POJO中。

显然,我可以使用Hibernate fetch个人资料,但是有一个障碍。以下是Hibernate的FetchMode.java所说的内容:

  / ** 
*使用外连接进行提取。等同于< tt> fetch =join< / tt> ;.
* /
public static final FetchMode JOIN = new FetchMode(JOIN);
/ **
*使用单独的选项热切地获取。等同于
*< tt> fetch =select< / tt> ;.
* /
public static final FetchMode SELECT = new FetchMode(SELECT);

/ **
*延迟取阅。等同于< tt> outer-join =false< / tt> ;.
* @deprecated use< tt> FetchMode.SELECT< / tt>
* /
public static final FetchMode LAZY = SELECT;

所以,LAZY不是很懒,它只是SELECT,这正是我试图避免。

是否有一个阻止集合被加载的获取模式?如果没有,有没有其他方法可以在特定的HQL查询过程中禁用集合加载?

我意识到完成此操作的传统方法是使用Criteria查询,但我需要对任意属性执行连接,据我所知,有没有标准的。

解决方案

首先,我相信hibernate的默认行为是使用延迟加载。然后,我相信他们误会了文档中的注释,并且 SELECT 实际上应该是一个懒惰的获取(如果您检查最新版本的hibernate的文档,'热切'的词将被删除)。

I asked a this question on the Hibernate forums, but did not get a full response, so I thought I'd repost it here. Here's the original question thread:

http://forum.hibernate.org/viewtopic.php?f=1&t=1008243&p=2438963#p2438963

In a nutshell, I have a custom HQL query that joins together two completely unrelated tables, and I'm trying to prevent the "O(N+1) selects" problem by disabling the loading of associated collections that exist in my Hibernate-mapped POJOs.

Apparently, I can use Hibernate fetch profiles for this, but there's a snag. Here's what Hibernate's FetchMode.java says:

/**
 * Fetch using an outer join. Equivalent to <tt>fetch="join"</tt>.
 */
public static final FetchMode JOIN = new FetchMode("JOIN");
/**
 * Fetch eagerly, using a separate select. Equivalent to
 * <tt>fetch="select"</tt>.
 */
public static final FetchMode SELECT = new FetchMode("SELECT");

/**
 * Fetch lazily. Equivalent to <tt>outer-join="false"</tt>.
  * @deprecated use <tt>FetchMode.SELECT</tt>
 */
public static final FetchMode LAZY = SELECT;

So, "LAZY" isn't really lazy, it's just "SELECT", which is exactly what I'm trying to avoid.

Is there a fetch mode that will prevent collections from being loaded ? If not, is there some other way to disable the loading of collections during the course of a specific HQL query ?

I realize that the conventional way to accomplish this is to use Criteria queries, but I need to do a join on an arbitrary property, and as far as I can tell, there are no Criteria for that.

解决方案

First, I believe the default behaviour of hibernate is to use lazy-loading. So check if it is not already the case.

Then, I believe they have mistaken the comments in the documentation, and SELECT should really be a lazy fetch (if you check the documentation of the newest version of hibernate, the 'eagerly' word is removed).

这篇关于哪个Hibernate FetchMode会导致集合被延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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