Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Setting fetch mode for arbitrary HQL
PostPosted: Wed Nov 17, 2010 8:07 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
Hi all,

Let's say that I have three Hibernate-mapped classes in my project: Cat, Dog, and Squirrel. Here are their relevant properties:
Code:
public class Squirrel {
    private Long squirrelId;
    ...
}

public class Cat {
    private Long catId;
    private String slaveName;
    private Set<Squirrel> enemies;
    ...
}

public class Dog {
    private Long dogId;
    private String ownerName;
    ....
}

The Cat.enemies association is mapped as non-lazy in my cat.hbm.xml file. This works very well, because usually, whenever I load a specific Cat, I also want to load all of its enemies... except in this one case, when I want to execute the following HQL:
Code:
select cat, dog from Cat cat, Dog dog where cat.slaveName = dog.ownerName

I expect this query to return tens of thousands of rows, and I do NOT want to load the cat.enemies associations in this case, since a). I'd be doing tens of thousands of extra selects, and b). I don't care about the enemies, and they take up a lot of memory, so I'd rather not load them at all.

What can I do ? The obvious answer is, "change the cat.enemies association to be lazy-loaded, duh", but I can't do that; the Cat class comes from a library that's used by lots of other projects in our codebase, and I don't have the right to modify it (and besides, most of the time I actually do want to load cat.enemies, anyway). Another answer might be, "use a Criteria query instead of HQL, and override the fetch mode by calling setFetchMode(...)". Unfortunately, there's no mapped relationship between Cat and Dog, so I don't know how to express the join above in terms of Criteria.

I appreciate your help...


Top
 Profile  
 
 Post subject: Re: Setting fetch mode for arbitrary HQL
PostPosted: Thu Nov 18, 2010 5:04 am 
Beginner
Beginner

Joined: Fri Aug 24, 2007 4:46 am
Posts: 36
Location: Bielefeld / Germany
Maybe you can use different FetchProfiles:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/performance.html#performance-fetching-profiles

HTH, Maik

_________________
jease.org - Java with Ease


Top
 Profile  
 
 Post subject: Re: Setting fetch mode for arbitrary HQL
PostPosted: Thu Nov 18, 2010 6:40 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
Thanks, Maik, that sounds like almost exactly what I need. However, I've discovered that I'm somewhat confused about fetch modes. Here's the relevant snippet from my (admittedly, obsolete) FetchMode.java:
Code:
   /**
    * 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 just does a separate select, which is exactly what I want to avoid :-( Am I missing something ?


Top
 Profile  
 
 Post subject: Re: Setting fetch mode for arbitrary HQL
PostPosted: Tue Nov 30, 2010 9:24 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
Just pinging this post to see if anyone can help me out with fetch modes: which fetch mode would PREVENT collections from being loaded at all ? But perhaps I should open up a separate thread ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.