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.  [ 4 posts ] 
Author Message
 Post subject: HQL: How to display super and sub-class columns in one query
PostPosted: Fri Sep 08, 2006 10:16 am 
Newbie

Joined: Fri Sep 08, 2006 9:55 am
Posts: 2
Hibernate version: 3.1

My question is, how can I in the same HQL query (and given that HQL doesn't support UNIONs), select columns from both a parent class and its sub-classes, and have nulls appear for subclass fields when dealing with rows of the base class.

For example, I have
- class Baseclass with field baseclass1
- class Subclass with field subclass1, with Subclass deriving from Baseclass

and I want in my query to select all instances of both Subclass & Baseclass, and:
- When dealing with an object of type Subclass, then show fields baseclass1 and subclass1

- When dealing with an object of type Baseclass, then show fields baseclass1 and null


The LEFT OUTER JOIN doesn't work with Base-Subclass constructs, so it can't be used in this case.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 7:35 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
query: from pkg.Parent

well, the query returns all parent and subclass instances!

and then check them by: instanceof

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 10, 2006 7:18 am 
Newbie

Joined: Fri Sep 08, 2006 9:55 am
Posts: 2
In my case, I'm not looking for objects but directly for column values. I need to populate a grid based on the result, and the whole point is to avoid writing code and simply have an HQL query in a configuration file driving the grid contents.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 10, 2006 5:40 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
well do something like:

Code:
select new Child(p.id, p.f1, p.f2, c.f1, c.f2)
from Parent as p outer join Child as c on p.id = c.id


by the way, I don't like it!

------------------
don't forget credits.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.