-->
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: How to make a SQL like this work?
PostPosted: Mon May 01, 2006 6:45 pm 
Newbie

Joined: Mon May 01, 2006 6:30 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.3
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
DB2 w8.2
The generated SQL (show_sql=true):
I have a SQL:
select temp1.attr1, temp1.attr2 from (select table1.attr1, table2.attr2 from table1, table2 as temp1 where table1.attr1=?)
Instead of using native SQL, how can I translate this SQL to HQL?
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 7:07 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate does not support subqueries in the from clause.

Your SQL will produce a two column result set, in which the first column is the given parameter (always the same), and the second column is the column "table2.attr2". Why not forget the parameter, seeing as it'll always be the same, and just issue the SQL "select attr2 from table2"? That's so simple that there's no point in writing HQL for it. Just use sql-query.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Sorry, maybe that's not a good example, look at this
PostPosted: Tue May 02, 2006 11:26 am 
Newbie

Joined: Mon May 01, 2006 6:30 pm
Posts: 2
tenwit wrote:
Hibernate does not support subqueries in the from clause.

Your SQL will produce a two column result set, in which the first column is the given parameter (always the same), and the second column is the column "table2.attr2". Why not forget the parameter, seeing as it'll always be the same, and just issue the SQL "select attr2 from table2"? That's so simple that there's no point in writing HQL for it. Just use sql-query.


Ok, my point is if I have a parameter in subselect, without native SQL, how can I make it work?
Let's change it to:
select temp.attra, temp.attrb, table3.attrx from (select table1.attra, table2.attrb as temp where table1.attrc=? and table1.attrd=table2.attre), table3 join ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 11:48 am 
Newbie

Joined: Wed Feb 16, 2005 5:56 am
Posts: 6
It would be good for the final HQL query you need if you could post the objects and mappings you are using. Maybe you dont need the subquery and you could simply use a inner join.

Greets

Sergio Berna
Open-VA, a fully accessible open source Validation Authority
http://www.open-va.org


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 6:08 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes, if you want to use HQL then you'll be expecting an object back, rather than a bunch of scalars. So your mappings, or the interesting bits from them, would help us come up with the exact solution. But in general, you can put subselects and parameters in HQL just like in SQL.
Code:
select a from YourClass a
join a.Child c
where c.Size in (select b.value from OtherClass b where b.Height = a.Width)

_________________
Code tags are your friend. Know them and use them.


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.