-->
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.  [ 5 posts ] 
Author Message
 Post subject: HQL - how can i make a union of two querys?
PostPosted: Sat Apr 17, 2004 7:02 pm 
Newbie

Joined: Tue Apr 13, 2004 10:32 pm
Posts: 3
Hi,

I need to make a query (with HQL) like this:

(SELECT OBJ1.ID, OBJ1.DESCRIPTION
FROM OBJECT1 OBJ1)
UNION
(SELECT OBJ2.ID, OBJ2.DESCRIPTION
FROM OBJECT1 OBJ1)

I didn't find a way with HQL to perform this type of query.

Can i perform this kind of union with HQL?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 17, 2004 7:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
HQL doesn't support UNION, use createSqlQuery() or session.connection().prepareStatement...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 18, 2004 8:27 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
I'm assuming:
Quote:
(SELECT OBJ1.ID, OBJ1.DESCRIPTION
FROM OBJECT1 OBJ1)
UNION
(SELECT OBJ2.ID, OBJ2.DESCRIPTION
FROM OBJECT1 OBJ1)

is supposed to read:
Quote:
(SELECT OBJ1.ID, OBJ1.DESCRIPTION
FROM OBJECT1 OBJ1)
UNION
(SELECT OBJ2.ID, OBJ2.DESCRIPTION
FROM OBJECT1 OBJ2)


If that is the case, then you can get Hibernate to kind of do a union in Java land, if (and only if) OBJ1 and OBJ2 have a common superclass (that no other classes have).
If they currently don't have a common superclass, you can do someting as simple as making them both implement the same tag (ie. empty) interface.

All you need is to do the query on the base class and Hibernate will do two separate SQL queries and return you one collection containing all the results.

Let me know if you use this method because I've not used it in a production situation and I'd be interested in hearing about any issues with it (as I do plan to use this technique eventually).

P.S. If you're doing the query in HQL land, make sure you specify the fully qualified classname of the base-class in the query.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 18, 2004 11:59 pm 
Newbie

Joined: Tue Apr 13, 2004 10:32 pm
Posts: 3
yes.

my mistake.

the query is:

(SELECT OBJ1.ID, OBJ1.DESCRIPTION
FROM OBJECT1 OBJ1)
UNION
(SELECT OBJ2.ID, OBJ2.DESCRIPTION
FROM OBJECT2 OBJ2)

I don't think that is a very good design idea in my particular business model to implement this kind of inheritance.

I think that I'll use traditional SQL to resolve this issue.

Thanks anyway


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 19, 2004 4:12 am 
Newbie

Joined: Fri Apr 16, 2004 9:27 am
Posts: 18
Location: Russia, Spb
Quote:
I don't think that is a very good design idea in my particular business model to implement this kind of inheritance.


May be that is't good idea. But sometimes are possible cases that needs UNION operation.

To Hibernate team: think about this.


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