Hibernate show real SQL [duplicate] Hibernate show real SQL [duplicate] java java

Hibernate show real SQL [duplicate]


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=DEBUGlog4j.logger.org.hibernate.type=TRACE

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

Reference


log4j.properties

log4j.logger.org.hibernate=INFO, hblog4j.logger.org.hibernate.SQL=DEBUGlog4j.logger.org.hibernate.type=TRACElog4j.logger.org.hibernate.hql.ast.AST=infolog4j.logger.org.hibernate.tool.hbm2ddl=warnlog4j.logger.org.hibernate.hql=debuglog4j.logger.org.hibernate.cache=infolog4j.logger.org.hibernate.jdbc=debuglog4j.appender.hb=org.apache.log4j.ConsoleAppenderlog4j.appender.hb.layout=org.apache.log4j.PatternLayoutlog4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%nlog4j.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"/>


If you can already see the SQL being printed, that means you have the code below in your hibernate.cfg.xml:

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

To print the bind parameters as well, add the following to your log4j.properties file:

log4j.logger.net.sf.hibernate.type=debug