-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to view generated SQL statement for HQL
PostPosted: Thu May 11, 2006 2:47 pm 
Newbie

Joined: Thu May 11, 2006 2:34 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.x

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Hi I am new to Hibernate. Knowing that I could set <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> in the configuration file to sql the generated sql statement. I am looking for another way in which after creating a Criteria/SQLQuery, I can invoke a method... to print generated sql statement to the console. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 4:53 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
set up log4j to output the logging to the console

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 5:15 pm 
Newbie

Joined: Thu May 11, 2006 2:34 pm
Posts: 4
As I have indicated in my previous post, I can set <show_sql> to true. Doing this way, if I want to see a generated insert sql statement, I actually need to insert a new row to a database table! Could I view a generated sql stmt without making any connection to a database? Could HQL be translated into sql statement on the fly?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 3:38 am 
Regular
Regular

Joined: Wed Apr 12, 2006 12:49 am
Posts: 105
Location: Malaysia
Maybe you can tryout org.hibernate.hql.QueryTranslator as I notice it has a method called "getSQLString()".


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 6:11 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
You could try something like:

Code:
// session is your hibernate Session
String hql = "here is your HQL query";
SessionFactoryImplementor sfi = (SessionFactoryImplementor) session.getSessionFactory();

//build a map of filters from SessionFactory
//you could use (Map)null instead of this if you don't use filters
Map<String, Filter> mf = new HashMap<String, Filter>();
Set<String> filterNames = sfi.getDefinedFilterNames();
for(String filterName : filterNames) {
   mf.put(filterName, session.getEnabledFilter(filterName));
}

QueryTranslator qt = sfi.getSettings().getQueryTranslatorFactory().createQueryTranslator(hql, hql, mf, sfi);
qt.compile((Map)null, false);

System.out.print("\n\n" + qt.getSQLString() + "\n\n");


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 4:55 pm 
Newbie

Joined: Thu May 11, 2006 2:34 pm
Posts: 4
This works. Thanks a lot guys


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