Search code examples
jakarta-eejpahqlnamed-query

javax.persistence NamedQuery not working as expected


I'm probably being dense about this but a query isn't returning the results I'm expecting...

I have two tables(entities): Properties and Landlords as follows:

Properties is made up of the fields (amongst others): Id, propertyRef and landLordsid (which is a many-to-one join to table Landlords).

Landlords is made up of the fields (amongst others): Id, landlordName and the one-to-many Collection for the join.

I want the query to return all the properties for the landlord as passed in via the Integer parameter 'landLord', e.g. if the parameter is 2 then filter for Properties.landLordsid that equals 2. This is the NamedQuery I'm currently using but it returns all properties and doesn't seem to filter.

SELECT p 
from Properties p 
JOIN p.landLordsid l 
WHERE l.id = :landLord

Any ideas what's wrong with the query?


Solution

  • Let hibernate do the joining for you:

    This HQL should work:

    from Properties p 
    where p.landLord.id = :landLord
    

    Note how the join is implied and that "select *" is also implied.