-->
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: Adding hints to SQL generated from SQL
PostPosted: Tue Dec 04, 2007 5:54 pm 
Newbie

Joined: Mon Oct 10, 2005 1:07 pm
Posts: 13
Hi,

I've read in a few posts that in order to add hints to SQL that is generated from HQL, one can use the Query.addComment() method. The problem is that the comment is added at the begininng of the query, and Oracle expects it after the SELECT keyword, e.g.

select /*+ FULL(emp_status) */ empname, status from
emp_status where status = 'P';

and this will not work:

/*+ FULL(emp_status) */ select empname, status from
emp_status where status = 'P';

does someone have a solution for this?

Thanks,
moshe.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 12:58 pm 
Regular
Regular

Joined: Tue Mar 22, 2005 2:27 am
Posts: 62
Hi Moshe,

There is no way to specify DB-specific SQL query hints when using HQL.
There was some discussion at some point of adding it as a feature, but nothing ever came of it.
http://forum.hibernate.org/viewtopic.php?t=928724

We hit this same issue a couple of years ago with Informix. (it uses comments after SELECT to specify hints just like Oracle does)
Rather than drop back down to using SQL queries as was the suggestion we received at the time, we decided to patch Hibernate to address the issue.

FYI, we modified org.hibernate.loader.Loader as follows...

Original Code
Code:
   private String prependComment(String sql, QueryParameters parameters) {
      String comment = parameters.getComment();
      if ( comment == null ) {
         return sql;
      }
      else {
         return new StringBuffer( comment.length() + sql.length() + 5 )
               .append( "/* " )
               .append( comment )
               .append( " */ " )
               .append( sql )
               .toString();
      }
   }


Patched Code:
Code:
   private String prependComment(String sql, QueryParameters parameters) {
      String comment = parameters.getComment();
      if ( comment == null ) {
         return sql;
      }
      else {
         return sql.replaceFirst("select ", "select /*" + comment + "*/ ");
      }
   }


Note that as a consequence of this, we have to re-apply this patch every time we upgrade Hibernate versions.

Regards,

Frank Grimes


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.