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...