I found some similar posts regarding this issue but no satisfatory answers. Is it possible to return an extra-lazy collection from a query? My use case is e-mails. I may want to show the number of, say, new messages from my contact list upon login. However, I have no need at that point to show any actual message subject, body, etc.
I know I can issue a count query and obtain the count without initializing any message collection. However, this will force callers of my message retrieval code to use two different methods to either get the actual collection or get the count. These two different methods will have very similar select queries that I would rather consolidate.
In short, I would like for the caller of my code to execute one single method:
List<Message> messages = magicCode.getMessages(UNREAD+FROM_CONTACTS); // this issues an HQL query
int count = messages.size();
In other words, the message collection needs to be extra lazy here.
If member collections can be extra lazily retrieved, why can't collections based on queries have the same capability?
Thanks.
|