Avoiding NULL object in HQL Query where clause

dbnullhqlnhibernate

I have an entity which might have a parent entity.
I want to run this query:

select entity where entity.parent.id = 9

some of the entity does not have parents (entity.parent = null) and N HIBERNATE Fails to run this query (QueryException – Could not resolve property)

How can I use HQL to get all the entities that has parents entities with id 9, avoiding the ones that the parent is null ?

(adding entity.parent is not null before the entity.parent.id = 9 results in the same exception)

There is an option to use a nested select statements but I don't think this is the most efficient solution.

Best Solution

You are missing a from clause in your HQL query. Try rewriting it like this:

from entity where entity.parent is not null and entity.parent.id = 9

Not sure, but probably the entity.parent is not null part is not necessary. I suppose NHibernate will handle this scenario.

Related Question