How can I write a Hibernate Criteria query, for a super-class, and check for a certain sub-class? Let's imagine we have the following classes all mapped up with Hibernate-JPA:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Bar {
@Id
@Column(name = "id")
private Long id;
}
@Entity
@PrimaryKeyJoinColumn(name="bar_id")
public class Foo extends Bar {
}
@Entity
@PrimaryKeyJoinColumn(name="bar_id")
public class Goo extends Bar {
}
When writing a Criteria query like this, I would like, for performance, to use a left-join with the sub-class:
getSession()
.createCriteria(Bar.class)
.createAlias("Foo", "foo", CriteriaSpecification.LEFT_JOIN)
.add(Restrictions.isNotNull("foo.bar_id"))
.list();
This fails, because the association path "Foo" doesn't work obviously, but it will illustrate what I want. Or is there another way of doing this type of query? I need the query to be performed on the superclass. If I would have done it in SQL it would look like this:
select b.*
from bar b left join foo f on f.bar_id = b.id
where f.bar_id is not null;
The SQL query above is just to illustrate what I mean, I know it would be easier to use a "normal" join in that specific case.