Hi,
I throw myself on the wisdom of the Hibernate forums!
I've got the following object hierarchy, mapped using InheritanceType.TABLE_PER_CLASS (can't use table-per-hierarchy, though that would solve this one problem):
A
|- B
|- C
Class A is not abstract. This gives me three tables: A, B and C.
Question is:
how can I query for objects of type A, without also including the other tables? The closest I've come is to use the class discriminator in the following form (JPA2 CriteriaBuilder):
Code:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<A> query = builder.createQuery(A.class);
Root<A> root = query.from(A.class);
query.where(builder.equal(root.type(), A.class));
return em.createQuery(query).getResultList();
This returns the correct result (A objects only), but the query string produced by Hibernate looks like this:
Code:
select A0_.id as id0_, ..., A0_.clazz_ as clazz_ from ( select id, ..., 0 as clazz_ from A union all select id, ..., 1 as clazz_ from B union all select id, ..., 2 as clazz_ from C ) A0_ where clazz_=0
So it's querying
every child table then immediately discarding all the results from them! I was hoping that, using the TPC strategy, Hibernate would be clever enough to only query the tables for the class types I was interested in. Executing as it does means terrible things for performance, as the tables in this case will be very large.
Alternatively - if anybody knows of a way to retrieve class types as part of a polymorphic query, that would also get me out of this bind. E.g. being able to do:
Code:
select id, class from A
And getting back from it arrays of [1, A.class], [2, B.class], ...
Any advice/suggestions would be hugely appreciated. Thanks!