spring 即使 sql_show=true,Hibernate 也不显示 sql 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7074481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:37:10  来源:igfitidea点击:

Hibernate not showing sql statement even with sql_show=true

hibernatespring

提问by Roy Chan

I am not sure why it doesn't show sql statement. I have it working before (on older spring, I am using 3 this time)

我不确定为什么它不显示 sql 语句。我以前用过(在较旧的春天,这次我用了 3 个)

In ApplicationContext I have :

在 ApplicationContext 我有:

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>my.model.*</value>
        </list>
    </property>
</bean>

In log4j:

在 log4j 中:

# Standrd System.out appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# package override setting
log4j.logger.org.hibernate.SQL=DEBUG, stdout
log4j.additivity.org.hibernate.SQL=false
log4j.logger.org.displaytag=INFO

log4j.rootLogger=DEBUG, stdout

Everything else seems fine, but it just doesn't show me the sql.

其他一切似乎都很好,但它只是没有向我显示 sql。

Did I miss anything?

我错过了什么吗?

(Or is it possible to print from SessionFactory from org.hibernate.cfg.Environment.getProperties()? It is not showing the show_sql, probably not even injected properly?)

(或者是否可以从 org.hibernate.cfg.Environment.getProperties() 的 SessionFactory 打印?它没有显示 show_sql,甚至可能没有正确注入?)

Please help Thanks in advance!

请帮助提前致谢!

采纳答案by dr jerry

make sure DEBUG is good, at one point hibernate logging changed from DEBUG to TRACE. Also make sure there is no threshold in your log4j.config. if you want also your arguments to be displayed include org.hibernate.type. You may also need to set org.hibernate.jdbc=TRACEor try org.hibernate=TRACEanalyse your needs and change back to proper levels per package.

确保调试良好,在某一时刻休眠日志从调试更改为跟踪。还要确保您的log4j.config. 如果您还希望显示您的参数包括org.hibernate.type. 您可能还需要设置org.hibernate.jdbc=TRACE或尝试org.hibernate=TRACE分析您的需求,并更改回每个包的适当级别。

回答by Ryan Stewart

You have hibernate.show_sql configured correctly. Where are you looking for the output? In any case, it's better to just forget about show_sql and use Hibernate's logging instead. It's much more flexible. Remove the "hibernate.show_sql" property entirely, and in your logging config use

您已正确配置 hibernate.show_sql。你在哪里寻找输出?无论如何,最好忘记 show_sql 并改用 Hibernate 的日志记录。它更加灵活。完全删除“hibernate.show_sql”属性,并在您的日志配置中使用

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

Note that there's no reason to mess with the additivity since everything is writing to the same appender, so this line doesn't do anything and should be removed:

请注意,没有理由搞乱可加性,因为所有内容都写入同一个 appender,因此该行不执行任何操作,应删除:

log4j.additivity.org.hibernate.SQL=false

回答by Renato Camargos

In my case, I managed to correct this by adding a argument on JBoss launch configuration:

就我而言,我设法通过在 JBoss 启动配置上添加参数来纠正此问题:

  1. Double click on "Red Hat JBoss EAP xxx".
  2. Click in "Open launch ocnfiguration" (tab "General Information").
  3. Add -Dorg.jboss.as.logging.per-deployment=falseon the end of "VM arguments" field.
  1. 双击“Red Hat JBoss EAP xxx”。
  2. 单击“打开启动配置”(选项卡“常规信息”)。
  3. 添加-Dorg.jboss.as.logging.per-deployment=false在“VM 参数”字段的末尾。

Worked for me.

对我来说有效。