-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: TPC inheritance, querying base types only
PostPosted: Fri Nov 02, 2012 12:51 am 
Newbie

Joined: Fri Nov 02, 2012 12:13 am
Posts: 4
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.