Search code examples
javahibernateormhqlsoft-delete

Whats the correct way to use HQL for data with delete flags


I have several Tables which have all a boolean field deleted. What is the best way to filter eager loaded collections (with hql) ie this:

from worker as w left join  w.taskSet as t 
where (w.deleted!=true or w.deleted is null )  
and (t.deleted!=true or t.deleted is null )

Wont return a worker instance if he has work which was deleted


Solution

  • The filtering is applied to the root entity being fetched (e.g. Worker) and collections won't be filtered out.

    You are better off defining a filter on both entities as well as one one-to-many associations.