Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help : Need to retrieve SQL Query from Criteria or HQL (SQL which is generated by Hibernate)

 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to retrieve or access the hibernate generated SQL query in Java Code and need to pass it some where else.

(Mostly I want to use it for the purpose of Reporting where if this is possible It will save lot of time in writting the complex SQL queries)

Is it possible some how ?

Thanks in advance.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate will log the SQL generates if you set the hibernate.show_sql property to true. You could configure an appender to pass this wherever you need it.
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

I know that I can see the generated SQL query by enabling hibernate.show_sql property.

But My problem is I want to use that generated SQL query.

I dont know how to configure the appender (Can you elaborate more on it?)

I have found out that Criterion has method

toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
Render the SQL fragment

But I dont know how to use it.
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Got solution but it seems little bit complex,

following code extracts the sql query out of criteria object :




Hoping for some simple solution.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic