I may not have been clear before. I understand how to deal with lazy collections.
What I am trying to do is specify which elements of the object graph I want to retrieve. I have a huge object graph, and I have a view which knows exactly what it needs, which is not very much at all.
I would like to select only the elements of the graph that I need to display. I don't actually care about lazy loading (proxies) at all -- I don't need lazy loading because my view would never load anything beyond what it had requested.
I think I know what I'm going to do, but I wanted to bounce it off you folks. Because it seems like it would be a common problem for web apps that need to present the user with a large collection of objects from which to select.
Let's say I have a Loan object that has a lazily loaded set of Property objects. The problem is, loading that collection takes a LOT of SQL (due to a lot of many-to-one and one-to-one relationships), and I find that I usually don't need more than a handful of attributes. So using Hibernate's standard lazy loading is sort of overkill in terms of SQL and memory.
Alternatively, I could run an HQL query where I specify exactly what I need. for example:
Code:
select
p.id,
p.propertyType,
p.name,
p.address
from Property p where p.portfolio = ?
The query runs very quickly and it returns everything I need. I'll probably end up doing this and writing a tag like
Code:
<my:find query="select..." id="properties"/>
I guess I was wondering
A. Whether this seems reasonable
B. Whether anyone else has done anything like this
C. Whether there is a way to do this programmatically, using Criteria
With Criteria, there is a way to setFetchMode("someAssociation", FetchMode.LAZY), but all that does is disable outer-join. I guess I'm wondering if there is a way to do something like FetchMode.DO_NOT_FETCH. There probably isn't. Just wanted to ask.