-->
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: SQL clauses in HQL queries
PostPosted: Tue Oct 05, 2004 9:13 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Hibernate version:2.1.2
Database: Ingres 2.6 SP2

When you write a HQL query, it is actually possible to drop back to SQL in the ORDER BY clause, like this:
Code:
SELECT object.createDate
FROM Object AS object
ORDER BY d_created


Notice the SQL column name in the ORDER BY clause, but a correct property reference in the SELECT clause.

Unfortunately, the query that we want to do this for is significantly more complex than that query, specifically, lots of tables are involved.

We need to use the SQL alias that HB generates in order to resolve the column name in our ORDER BY clause.

The current way we do this is:
- construct the HQL query without the ORDER BY clause
- run the unit test
- check the SQL generated
- embed the SQL alias into the HQL query in the ORDER BY clause

This is pretty dodgy (not that injecting SQL into the HQL is particularly elegant to begin with), and it causes problems when we modify the HQL.

Is there any way to either force HB to use given SQL aliases, or predict what aliases it will use?

P.S. The reason we need to use SQL is we need to stomp the time portion of the d_created date column so objects created on the same day are considered equal. Does anyone know a way of doing this in HB, other than by using SQL in the HQL for the ORDER BY clause (at the moment, we want to avoid doing the ordering of the result set in Java land if we can).

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 10:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I feel uncomfortable with what you are doing. Yes hibernate will pass it through if it cannot interpret it but its dodgy alright. Having said that I must not understand why you are finding it necesary to use column directly.
Why can't you use...
Code:
SELECT object.createDate
FROM Object AS object
ORDER BY object.createDate


Assuming that the field d_created is mapped to Object.getCreatedDate()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 10:08 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Yeah, but as I explained, we need to apply a SQL function to the order by column:
Code:
SELECT object.createDate
FROM Object AS object
ORDER BY [i]ingresSpecificFunctionTheExtractsThings(DATE_SPECIFIER, d_created)[/i]


Would it be possible to use that HQL substitutions thing-a-majig to make Hibernate understand ingresSpecificFunctionTheExtractsThings()?

_________________
Cheers,
Shorn.


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.