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

141 views Asked by At

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

2

There are 2 answers

0
Vlad Mihalcea On BEST ANSWER

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.

0
sherif On

This solution worked for me



@FilterDef(name="NOT_DELETED")
@Filters({
    @Filter(name="NOT_DELETED", condition="not deleted or deleted is null")
})
@Entity
...
   @Column
   Boolean deleted
...



Usage

session.enableFilter("NOT_DELETED");