My situation is this:
We have a superclass, “MyAbstractEntity”
@Entity
@Table(name = "MyTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("FOO")
public abstract class MyAbstractEntity implements Comparable<MyAbstractEntity> {
This class in turn has quite a few subclasses, looking like this:
@Entity
@DiscriminatorValue("BAR")
public class MyEntity extends MyAbstractEntity implements MyInterface {
About half of the subclasses implement “MyInterface”.
I have a Criteria in which I am interested in getting only objects from a class implementing that interface.
When I define my Criteria like this:
Criteria criteria = getSession().createCriteria(MyInterface.class)
Hibernate turns around and generates one DB-call for each implementation of the interface and the DB-guys goes nuts. I was half expecting Hibernate to translate this into something that used an “in” clause on the DISCRIMINATOR column rather than creating a separate call for each implementation.
I’ve tried googling around for this, but unfortunately my results get polluted by the fact that my query contains “hibernate”, “criteria” and “interface” and I keep bumping into pages that either explain the API or demonstrates basic examples.
I am aware that I could create the Criteria on “MyAbstractEntity” rather than “MyInterface” and remove unwanted results but I want this filtering to happen in the database.
Is there no way to tell Hibernate that I want everything in one single db-call?
I could not get Nizet’s solution to work so my slightly dirty way of doing it was:
where discriminatorValues is a static field initialized in a static block by using a Spring helper class.
Not the kind of solution I wanted to go for, but the resulting SQL looks like I wanted it to…
(and new implementations of the interface will be included in the query with no further action from future devs)