-->
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.  [ 32 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: TOP-N Query possible with HQL?
PostPosted: Mon Aug 02, 2004 9:59 pm 
Beginner
Beginner

Joined: Mon Dec 01, 2003 8:48 pm
Posts: 47
Location: Texas, United States
Here is my Oracle TOP-N Query:

Code:
SELECT * FROM (SELECT * FROM ul_common_log_event WHERE application_name = 'Configuration' ORDER BY cle_id DESC) WHERE ROWNUM <= 500


This query returns the latest 500 records, instead of the first 500.

I tried this in HQL:

Code:
from CommonLogEvent cle (from CommonLogEvent cle where cle.applicationName = 'Configuration' ) WHERE ROWNUM <= 500


I got this error from Hiberante:

net.sf.hibernate.QueryException: unexpected token: ( [from CommonLogEvent cle (from CommonLogEvent cle where cle.applicationName = 'Configuration' ) WHERE ROWNUM <= 500]

Is this possible to do in HQL?

Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 4:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
session.createQuery("from CommonLogEvent cle where cle.applicationName = 'Configuration' ").setMaxResults(500).list()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 10:48 am 
Beginner
Beginner

Joined: Mon Dec 01, 2003 8:48 pm
Posts: 47
Location: Texas, United States
Thanks for the reply.

The setMaxResults(500) returns the first 500 records found. The purpose of the TOP-N query is to return the latest 500 records.

I need the latest 500, not the first 500. Is this possible with HQL?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 10:55 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Order By

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:20 pm 
Newbie

Joined: Tue Aug 03, 2004 12:18 pm
Posts: 11
Does this work in hibernate version 2.0? We can't seem to get it to limit the number of returned rows.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:21 pm 
Newbie

Joined: Tue Aug 03, 2004 12:18 pm
Posts: 11
I'm sorry. does it work in version 2.1.4? That's the version that we are using.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:21 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
"My car doesn't work."

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:36 pm 
Newbie

Joined: Tue Aug 03, 2004 12:18 pm
Posts: 11
I don't understand your reply Christian. Are you saying that it is broken in version 2.1.4?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:38 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I was turning your statement into the equivalent for a car mechanic. What do you think would be the reply if you say this to a car mechanic?


(Man, is it the heat or what...)

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:47 pm 
Newbie

Joined: Tue Aug 03, 2004 12:18 pm
Posts: 11
Christian, I don't need a smart ass remark. All I'm asking is whether you claim that setMaxResults(500) is in fact supported in version 2.1.4. If you say yes, then I won't bother you. We'll figure out why it's not working for us. But my guy says that the current hibernate code he looked at couldn't possibly support the syntax.

And now I see that you just reject bugs saying that it couldn't possibly be. Without even checking the code.

Not much on the support side I see.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:48 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
So, I should start looking for a problem, which doesn't exist for any other user, just because "your guy" thinks that Hibernate "doesn't do anything" and the API is just fake?

Give me something to work with. This is just wasting time.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:51 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 2:15 pm
Posts: 35
hey smith,

I would check if you are using a good driver for this.. it seems like you are.. because earlier you said it does return 500, but they are not what you want..

So it seems like 2.1.4 is not broken.. I use order by all the time and it works great for me.

You might want to take a look here: http://www.hibernate.org/hib_docs/refer ... l-ordering

Also, at the end of the documentation, they have bunch of queries you can look at to see what you need to do.


I think hibernate is pretty well supported, esp if Christian is here answering questions. Plus their documentation is really good.

HTH,
Pritpal Dhaliwal


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 1:10 pm 
Beginner
Beginner

Joined: Mon Dec 01, 2003 8:48 pm
Posts: 47
Location: Texas, United States
The following query returns the latest 500 rows:

Code:
SELECT * FROM (SELECT * FROM ul_common_log_event WHERE application_name = 'Configuration' ORDER BY cle_id DESC) WHERE ROWNUM <= 500


This HQL query...
Code:
from CommonLogEvent cle where cle.componentIdentification.applicationName = 'Configuration' order by cle.id
with Query q = session.createQuery(query).setMaxResults(500);

produces this Oracle SQL, and returns no records... I'm not sure why two rowum statements are being created.

Code:
select * from ( select row_.*, rownum rownum_ from ( select commonloge0_.cle_id as cle_id, commonloge0_.observed_time as observed2_, commonloge0_.version as version, commonloge0_.situation as situation, commonloge0_.message as message, commonloge0_.status as status, commonloge0_.severity as severity, commonloge0_.log_level as log_level, commonloge0_.address_type as address_9_, commonloge0_.address_name as address10_, commonloge0_.component_name as compone11_, commonloge0_.application_name as applica12_, commonloge0_.instance_name as instanc13_, commonloge0_.thread_name as thread_14_, commonloge0_.environment_name as environ15_ from UL_COMMON_LOG_EVENT commonloge0_ where (commonloge0_.application_name='Configuration' ) order by  commonloge0_.cle_id ) row_ where rownum <= ?) where rownum_ > ?


Unfortunately, adding the order by to my HQL does not do the trick. BTW I am using Hiberante 2.1.1. I know that Hibernate supports sub-selects, so I am hoping there is a way to reproduce my Oracle TOP-N query


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 1:12 pm 
Newbie

Joined: Tue Aug 03, 2004 12:18 pm
Posts: 11
subpaul,

All I asked was does 2.1.4 support the setMaxResults() syntax. We are having trouble when the table size is large. It has nothing to do with the order by. That works fine. We're just having problems limiting the size of the returned result set. We also can't see anything in the query that actually goes to the database that would limit the number of rows in the resultset.

That leads me to beleive that the database is returning all the rows, and that hibernate is somehow limiting the population of the returned list(). The problem is that we can't find the place in the hibernate code that does this limiting.

Now, my guy tells me that restarting the server several times, and it's now working... who knows why these things don't work... :-). I would still like to know where in the hibernate code this is handled, since it doesn't seem to be handled in the SQL query itself.

I'm not complaining about the product. I'm just saying that all I asked was a simple question. I got a cryptic answer.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 1:14 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There have been fixes to the OracleDialect (I assume you use Oracle8) recently, try with 2.1.4 and the current CVS (or wait until I'll release 2.1.5 today).

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 32 posts ]  Go to page 1, 2, 3  Next

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.