It is not clear what you want to do.
First of all, since Foo inherits from Bar, finding instances of Bar automatically returns instances of Foo. Hibernate will take care of joining tables by itself.
Secondly: your SQL query is really strange. You make a left join (this means you are looking for bars that might not have an associated foo), but you also have a place where foo.bar_id is not null. This is actually an inner join and can be rewritten as
select b.* from bar b inner join foo f on f.bar_id = b.id
If you want to search for Foos and Foos, use Criteria with Foo as the root object:
getSession() .createCriteria(Foo.class) .list();
You will get instances of Foo, but since Foo extends Bar, these instances of Foo are also instances of Bar. This is what inheritance is.
Now, if you create a Criteria instance dynamically and at some point realize that the search should return only Foo instances, you should use the implicit class property:
Criteria c = getSession().createCriteria(Bar.class, "bar") // ... if (limitToFoos) { c.add(Restrictions.eq("bar.class", Foo.class)); }
source share