Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate show real SQL [duplicate]

if I set

<property name="show_sql">true</property>

in my hibernate.cfg.xml configuration file in the console I can see the SQL.

But it's not real SQL... Can I see the SQL code that will be passed directly to database?

Example:

I see

select this_.code from true.employee this_ where this_.code=?

Can I see

select employee.code from employee where employee.code=12

the real SQL?

like image 515
Tommaso Taruffi Avatar asked Mar 29 '10 09:03

Tommaso Taruffi


People also ask

What does hibernate show_sql true mean?

show_sql to true tells hibernate to Write all SQL statements to console. This is an alternative to setting the log category org. hibernate. SQL to debug.

How do you find the origin of a query?

How to find out the origin of the query. The logged query above contains a comment that allows to identify in most cases the origin of the query: if the query is due to a load by ID the comment is /* load your.entity.Name */ , if it's a named query then the comment will contain the name of the query.


2 Answers

Can I see (...) the real SQL

If you want to see the SQL sent directly to the database (that is formatted similar to your example), you'll have to use some kind of jdbc driver proxy like P6Spy (or log4jdbc).

Alternatively you can enable logging of the following categories (using a log4j.properties file here):

log4j.logger.org.hibernate.SQL=DEBUG log4j.logger.org.hibernate.type=TRACE 

The first is equivalent to hibernate.show_sql=true, the second prints the bound parameters among other things.

Reference

  • Hibernate 3.5 Core Documentation
    • 3.5. Logging
  • Hibernate 4.1 Core Documentation
    • 4.1. Logging
like image 171
Pascal Thivent Avatar answered Sep 29 '22 05:09

Pascal Thivent


log4j.properties

log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug

log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE

hibernate.cfg.xml

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

persistence.xml

Some frameworks use persistence.xml:

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
like image 25
Tommaso Taruffi Avatar answered Sep 29 '22 04:09

Tommaso Taruffi