-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL to support MySQL and SQLServer to count from Query obj
PostPosted: Thu Oct 02, 2008 4:55 pm 
Newbie

Joined: Thu Oct 02, 2008 4:05 pm
Posts: 3
Hibernate version:
3.2.5

Name and version of the database you are using:
- MySQL 5.0.41-community-nt
- SQLServer 2005

The ideal query sql:
Code:
select count(slide_id) from (
select distinct f.folder_slide_id, s.slide_id
from space_slide as s
   left join space_folder_slide as f on (f.slide_id = s.slide_id)
   left join space_attribute_value as a on (a.slide_id = s.slide_id)
where s.is_archived = 0
and
(s.slide_id not in (select slide_id from space_folder_slide)
   and (s.external_ref like '%win%'  or a.value like '%win %'))
or
(f.folder_id in (select folder_id from space_folder where space_folder.is_resource_name = 0)
   and  (s.external_ref like '%win%'  or a.value like '%win%'))
group by s.slide_id, f.folder_slide_id) as res_count;


We need to support both database server.

I've read pretty much all the pages of the forum to find a solution on this, but the only solution found were:

- Using Criteria API with the Projection call, but we use Query, not Criteria
- Using ScrollableResults and getting to last row and then getting the row number, but this causes a performance issue on MySQL.

This is the hibernate query I tried:
Code:
select count(s.id) from (
    select distinct f.id, s.id 
    from Slide s
        left join s.folders f
        left join s.attributeValues a 
    where s.archived = false
        and (s.folders.size = 0 and (s.filename like '%win%' or s.externalSlideReference like '%win%' or f.externalSlideReference like '%win%' or a.value like '%win%'))
        or ( f.folder.id in (6, 4, 7, 10, 11, 3, 9) and (s.filename like '%win%' or s.externalSlideReference like '%win%' or f.externalSlideReference like '%win%' or a.value like '%win%'))
    group by s.id, f.id) res_count


But Hibernate doesn't support select queries as from clause.

What can I do to fix this ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 11:56 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Look at the last posting in this topic

http://hibernar.org/foro/viewtopic.php?f=4&t=9

where we discuss a similar concern.

I believe this is a problem with the way Hibernate translates aliases of aggregated functions into SQL, which not all databases tolerate.

_________________
Gonzalo Díaz


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