-->
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.  [ 3 posts ] 
Author Message
 Post subject: Complex HQL queries
PostPosted: Tue Aug 22, 2006 9:20 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
I have a complex query, which involves aggregate funcions and runtime parameters. The same query needs to be executed with different ORDER BY clauses, which are specified at runtime.

Because of the complexity of the query, I would like to avoid using CriteriaQuery and my question is about alternative ways to solve this problem. I lay out my ideas below.

I wonder if it's possible to build HQL queries from XML fragments. In this case I can create an HQL query, which contains the body of the target query and leaves out the ORDER BY clause. I could then reference or "include" this in the final query, which will also specify the ORDER BY clause.
1. I quess I could do this by using XInclude, but I wonder if HQL provides a similar mechanism.

Another idea is to map this complex query to an Entity class using <subselect> feature.
2. But how do I pass runtime parameters in <subselect>?
3. Would I be able to order collections of such mapped entities using createFilter()?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 11:49 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Between Projections.sqlProjection and Restrictions.sqlRestriction, there should be very, very little that criteria queries can't handle. There are certain conjoined queries that can only be handled by subselects, which is a pain, but everything else that I can think of at the moment can be dealt with using criteria.

  1. I don't think so. If you're not afraid of hackery, you can use kludges like
    Code:
    session.createQuery(unorderedQuery.getQueryString() + " " + orderByString)
  2. You can't.
  3. Nope.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 10:47 am 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
tenwit,

I didn't want to use criteria queries not because they can't support complex queries, but because it is hard to read and maintain large criteria queries. It is a lot easier to read a large HQL query than a large criteria query.

According to your response the only clean option I have is using criteria query API. If anybody knows of a good alternative, please let me know.

I still think that being able to compose and HQL query from query fragments similar to what I was suggesting in my question 1.

Thanks.


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