I have a number of classes mapped as an inheritance hierarchy into a single table.
I would like to use the following Criteria
query but this results in a ClassCastException
since Class
cannot be cast to String
.
Set<Class> childClasses = new HashSet<>();
childClasses.add(Child1.class);
childClasses.add(Child2.class);
session.createCriteria(Parent.class)
.add(Restrictions.in("class", childClasses)
.list();
I note that Hibernate does support specifying a single class using Restrictions.eq("class", childClass)
so I can workaround by using a Disjunction
'. I also know that this would work if my resriction was based on the discriminator strings for each subclass but I would prefer not to use these.
It is possible to use Criteria
in this manner? The accepted answer to this question suggests that it works when the class is a property of the class you are basing your Criteria
on but it doesn't seem to work in the case I've shown above.