Which Hibernate FetchMode will cause collections to be lazy-loaded?
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. He开发者_C百科re'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).
精彩评论