-->
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: Problem when using count with distinct
PostPosted: Tue May 02, 2006 6:29 am 
Newbie

Joined: Tue May 02, 2006 6:06 am
Posts: 2
i have a problem with hibernate; I want to get the row count of this named query:

Code:
select distinct table.a, table.b from MyTable table where table.a = :val


where a and b are primary key columns of MyTable.

So, as i usually do with sql, I tried

Code:
select count(*) from
(select distinct table.a, table.b from MyTable table
where table.a = :val)


but hibernate refuse to execute this query, saying :

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ( near line 3, column 46)

I tried a lot of tricks (as select count(distinct table.a, table.b). for example), but none did satisfy me. The only way to do that on hql seems something like :

Code:
Query myQuery = session.createQuery("select distinct table.a, table.b from MyTable table where table.a = :val").setParameter("val", "...");
int count = myQuery.list().size;


but i'm afraid hibernate map the whole list when getting its size, which may be huge, whereas i'm only interest by its size.

Hibernate version:
3.1

Name and version of the database you are using:
Oracle 9i


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 3:32 pm 
Newbie

Joined: Tue May 02, 2006 3:17 pm
Posts: 3
I have exactly the same problem. This is an easy query for SQL, surprised that I cannot do this in HQL. For now I must list the results and use the size() method.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 3:49 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Maybe the trick in the link will help you http://www.hibernate.org/118.html#A2


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 9:48 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
If your database or JDBC driver supports this, you could use
ScrollableResults. This does not actually iterate over the whole
result set when you request the last page and get the row number.

I think this will allow you to run the distinct query and get the size of
the result set without costing too much time and without mapping the
results back into objects.

See the code fragment below:
Code:
                    ScrollableResults sr = q.scroll();
                    sr.last();
                    int size = sr.getRowNumber();


This fragment is typically used in pagination, but the principal would be
the same, I think.

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 12:24 pm 
Newbie

Joined: Tue May 02, 2006 6:06 am
Posts: 2
bkmr_77 wrote:
Maybe the trick in the link will help you http://www.hibernate.org/118.html#A2


You know, looking at the doc is the first thing i'v done (the first request i wrote is what it is said in the documentation); If it had solved my problem i wouldn't have post any message on this board

bonnyr wrote:
If your database or JDBC driver supports this, you could use
ScrollableResults. This does not actually iterate over the whole
result set when you request the last page and get the row number.

I think this will allow you to run the distinct query and get the size of
the result set without costing too much time and without mapping the
results back into objects.

See the code fragment below:
Code:
                    ScrollableResults sr = q.scroll();
                    sr.last();
                    int size = sr.getRowNumber();


This fragment is typically used in pagination, but the principal would be
the same, I think.


i'll will try what you said, it looks better than what i did, at least until this query will be supported by hibernate.


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.