I am attempting to use HQL to retrieve a list of objects, but am having difficulty.
I want to get information on all my properties, along with the address and tenant information.
Code:
select house
from Property as house
left join fetch house.Address as address
left join fetch address.tenant as tenant
now I only want to include information for tenants that make more than $50k, so I modify it to this:
Code:
select house
from Property as house
left join fetch house.Address as address
left join fetch address.tenant as tenant
where
tenant.income > 50000
But of course this causes the query to not return the entire property object if there is no tenant associated with the property that has an income greater than 50k.
In SQL I would add a clause to the join against tenant to restrict the set of rows that come from the tenant table rather than a where clause which removes the entire row of data; so trying something similar in HQL I arrive at:
Code:
select house
from Property as house
left join fetch house.Address as address
left join fetch address.tenant as tenant WITH tenant.income > 50000
However this returns the error:
with-clause not allowed on fetched associations; use filters
From the reading I have done it seems that filters are also applied as "WHERE" clauses which seems to indicate that they are not the solution I need.
So -
Am I misunderstanding how filters work and that they can be applied to the join clause?
Is there a way in HQL to achieve the query I want?
Thanks all in advance for any assistance.