Hibernate-4 show SQL code that is generated

我是研究僧i 提交于 2019-12-23 05:42:12

问题


I created a Maven project in my eclipse IDE and trying to write simple hibernate program. But I am not able to see the final query that is generated by hibernate which also includes the bind parameters.

I also followed the post mentioned here : Hibernate show real SQL , but it did not help.

I have below configurations:

In my hibernate.cfg.xml file I have:

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

I also placed log4j.properties file with its contents:

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.logger.net.sf.hibernate.type=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

Both hibernate.cfg.xml and log4j.properties are placed at path :

MyProject/src/main/java/log4j.properties

But still when I run a small program I am not able to see the bind parameters that are added by hibernate, I am just seeing below results:

Hibernate: 
    select
        this_.ID as ID1_0_0_,
        this_.NAME as NAME2_0_0_
    from
        MY_TABLE this_ 

I am using Hibernate-4.3


回答1:


Adding SLF4J dependency in pom.xml has fixed my issue:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.6</version>
    </dependency>



回答2:


Change this line:

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

to:

log4j.logger.org.hibernate.type=trace

And you should get the bind parameter.




回答3:


The log4j.properties file must be located in the following directory:

MyProject/src/main/resources/log4j.properties

Thus, log4j will find this file in the classpath without any additional configuration.


See also Introduction to the Standard Directory Layout in the Apache Maven webpage.

You may want to see the next question: Logging server logs to one file and SQL logs to another.



来源:https://stackoverflow.com/questions/25520633/hibernate-4-show-sql-code-that-is-generated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!