-->
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.  [ 3 posts ] 
Author Message
 Post subject: Efficient way to determine class of a mapped object w/o load
PostPosted: Wed Dec 07, 2005 1:05 am 
Newbie

Joined: Tue Sep 30, 2003 2:00 pm
Posts: 11
Hibernate version:
2.1.7


Is there an efficient way in HQL to determine the class of a polymorphic mapped object, given the id, without loading the whole object?


for instance, lets say you have this hierarchy:

Code:
<class name="A">
   <id name="id".../>
   <joined-subclass name="B">
      <key column="id"/>
   </joined-subclass>
   <joined-subclass name="B">
      <key column="id"/>
   </joined-subclass>
</class>


can you get the class type of object id'd by "someID" without doing

Code:
Class clazz = session.load(A.class, "someID").class;


and without resorting to an ugly raw sql statement like:

Code:
select case
    when c.id is not null then 'C'
    when b.id is not null then 'B'
    when a.id is not null then 'A'
    as clazz
from A a
left outer join B b on a.id = b.id
left outer join C c on a.id = c.id
where a.id = "someID" 


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 3:07 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Try something like "SELECT foo.class from Customer foo WHERE foo.id = 5"

... but there's no way to get that w/ out going to the database. In the case of a mapped class w/out inheritance, you can find that quite efficiently via the Configuration API.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 2:02 pm 
Newbie

Joined: Tue Sep 30, 2003 2:00 pm
Posts: 11
dennisbyrne wrote:
Try something like "SELECT foo.class from Customer foo WHERE foo.id = 5"

... but there's no way to get that w/ out going to the database. In the case of a mapped class w/out inheritance, you can find that quite efficiently via the Configuration API.


I tried selecting on class in HQL but that throws an exception, so I figured it was illegal.

I ended up using a jdbc call with a join on all the subclass tables and a case statement.

Thanks.


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

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.