-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: get generated sql at runtime
PostPosted: Tue Nov 21, 2006 11:03 am 
Newbie

Joined: Mon May 10, 2004 5:45 pm
Posts: 9
I have a requirement in my current project to get access to the generated SQL at runtime. I am using Hibernate v3. Does anybody know if this is possible and where in the API one can do this?

thanks,
ray


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 12:43 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:30 pm
Posts: 46
Location: Fortaleza, Brazil
Use the hibernate.show_sql property:

http://www.hibernate.org/hib_docs/v3/re ... n-optional

HTH,
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 21, 2006 10:28 pm 
Newbie

Joined: Mon May 10, 2004 5:45 pm
Posts: 9
the hibernate.show_sql just logs the SQL statements to the console. I want to get access to the String in my code and store it in a database. Is there a way to do that?

-ray


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 5:23 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hey,
you can get the sql queries by calling, getQueryString() method of Query class. you will get whole sql except the runtime parameters

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 11:39 am 
Newbie

Joined: Mon May 10, 2004 5:45 pm
Posts: 9
Unfortunately, the getQueryString() method returns the HQL query string and not the SQL query string


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:35 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
I think you can enable DEBUG on the log4j system of Hibernate and use JDBCAppender or some custom Appender to capture the native SQL.

Anyway this option will make your app very slow (hibernate generates a lot of debug).

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:45 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
andresgr wrote:
I think you can enable DEBUG on the log4j system of Hibernate and use JDBCAppender or some custom Appender to capture the native SQL.

Anyway this option will make your app very slow (hibernate generates a lot of debug).


Good idea there andre. You can set the JDBCAppender to only log the org.hibernate.sql package, I believe that will get you only the sql statements. Of course it will be prepended by "Hibernate: ", not sure if you can use a ConversionPattern or what not to get rid of that.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:52 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Oh, sure, I didn't realize you can enable the DEBUG only for a given package. You're right,

About the "hibernate" prefix it only occurs when you use "show_sql=true". The class "SQL" debug's the SQL string directly (SQL:393, in my hibernate 3.2 CR4).

Regards

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:06 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
There ya go. I never thought about using log4j to log SQL statments to a database, never had cause to use the JDBCAppender. Now I'm going to scuttle off to see how this works, it would be nice to log the SQL in a database on-demand with timestamps to tweak optimizations.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:11 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Anyhoo, back to the OP:

Use log4j with either the JDBCAppender and read from that table or use the WriterAppender and create a Writer which will synchronize access to a (thread-bound?) data stream and read from there.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:12 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Ananasi, i tried to use JDBCAppender and have some problems with it... if you want to talk about it send me email at angoro -at- gmail

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:32 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I'll give it a quick whirl when I get the chance (crunch time at the moment), but will keep this thread bookmarked for when I encounter a problem with the JDBCAppender.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 2:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
org.hibernate.Interceptor.onPrepareStatement()

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 2:15 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Hey! the one who knows, knows (as we say in spain...)

Regards.

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 7:42 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can get it from system tables, there are many usefull views and features for performance tuning (statistics, tracing, query plans). oracle store this stuff in v$sqltext view


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.