Hi,
I'm trying to write a HQL query which will return a component object (Address) which contains a collection of Strings (nonspecificAddressLines), which are by default lazily fetched. I would like this collection to be join fetched in the query.
If I just select the entity (Account) which is the ultimate parent of Address, there's no problem. The query looks like this
Code:
from Account account
left join fetch account.person.contactDetails.address.nonspecificAddressLines
where account.id = ?
So, I would have thought that just selecting the Address would have been as simple as the following:
Code:
select account.person.contactDetails.address
from Account account
left join fetch account.person.contactDetails.address.nonspecificAddressLines
where account.id=?
Unfortunately, it doesn't work. I get the following exception:
Code:
org.springframework.orm.hibernate3.HibernateQueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
If I leave out the left join fetch, the nonspecificAddressLines collection remains null. It doesn't even get a PersistentList stub.
Ok, so I have a vague idea what's going on here, I think. You need to have an entity in the select clause for hibernate to correctly execute the query necessary to populate the collection.
What I want to know is, is there any workaround? Is there any way I can construct a single query which will return an Address object with a fully populated nonspecificAddressLines collection? I know I could just get the Account entity and pull the address from that, but I'd rather keep that work in the query if it's possible in order to be consistent with my approach for other queries.
Thanks,
Mark
Edit: I should say that I'm using Hibernate 3.2.2