Hibernate-4显示生成的SQL代码[英] Hibernate-4 show SQL code that is generated

本文是小编为大家收集整理的关于Hibernate-4显示生成的SQL代码的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在Eclipse IDE中创建了一个Maven项目,并尝试编写简单的Hibernate程序.但是我看不到Hibernate生成的最终查询,还包括绑定参数.

我还遵循此处提到的帖子: Hibernate Show real SQL 帮助.

我具有以下配置:

在我的hibernate.cfg.xml文件中,我有:

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

我还放置了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.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

hibernate.cfg.xml和log4j.properties都放置在路径上:

MyProject/src/main/java/log4j.properties

但是,当我运行一个小程序时,我将无法看到Hibernate添加的绑定参数,我只是看到以下结果:

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

我正在使用Hibernate-4.3

推荐答案

添加SLF4J pom.xml中的依赖性已解决我的问题:

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

其他推荐答案

更改此行:

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

to:

log4j.logger.org.hibernate.type=trace

您应该获得绑定参数.

其他推荐答案

log4j.properties 必须位于以下目录中:

MyProject/src/main/resources/log4j.properties

因此,log4j将在Class Path中找到此文件,而无需任何其他配置.


另请参见 标准目录布局 apache maven webpage.

您可能想查看下一个问题: 本文地址:https://www.itbaoku.cn/post/1574976.html

问题描述

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

推荐答案

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>

其他推荐答案

Change this line:

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

to:

log4j.logger.org.hibernate.type=trace

And you should get the bind parameter.

其他推荐答案

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.