无法使用 Spring JPA Vendor Adapter 使休眠停止显示 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/679518/
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 03:50:26  来源:igfitidea点击:

Can't make hibernate stop showing SQL using Spring JPA Vendor Adapter

hibernatespringjpa

提问by Mojo

Hibernate is continuing to spew SQL traces to stdout, and I can't figure out how to change a Hibernate configuration property when it's hidden behind a JPA adapter. This is the Spring bean for the entityManagerFactory:

Hibernate 继续向 stdout 发送 SQL 跟踪,当 Hibernate 配置属性隐藏在 JPA 适配器后面时,我无法弄清楚如何更改它。这是 entityManagerFactory 的 Spring bean:

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="ssapDataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
            <property name="showSql" value="false"/>
        </bean>
    </property>
</bean>

Even with the showSql property set to false, Hibernate keeps printing SQL.

即使将 showSql 属性设置为 false,Hibernate 也会继续打印 SQL。

I've tried making a hibernate.properties file in my classpath with "hibernate.show_sql=false", but it didn't pick that up either.

我试过用“hibernate.show_sql=false”在我的类路径中创建一个 hibernate.properties 文件,但它也没有选择。

回答by NA.

Try setting it in persistance.xml

尝试在persistance.xml中设置

<persistence>
  <persistence-unit name="PU">
    <properties>
      <property name="hibernate.show_sql" value="false"/>
    </properties>
  </persistence-unit>
</persistence>

回答by navee

<property name="jpaProperties">
    <props>
        <prop key="hibernate.show_sql">false</prop>
    </props>
</property>

this will also work

这也行

回答by Nils Wloka

As far as I know, Hibernate will also log SQL statements if logging for org.hibernate.SQLhappens at DEBUGor ALLlevel, so you could try disabling that (for example with log4j.logger.org.hibernate.SQL=infowhen using Log4J).

据我所知,如果日志记录org.hibernate.SQL发生在DEBUGALL级别,Hibernate 也会记录 SQL 语句,因此您可以尝试禁用它(例如log4j.logger.org.hibernate.SQL=info在使用 Log4J 时)。

回答by Nils Wloka

If you are using spring make sure that you don't have the showSql property set to true

如果您使用的是 spring,请确保您没有将 showSql 属性设置为 true

I was doing this myself

我自己在做这个

<bean id="vendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false"/>
</bean>

回答by Mohammed Rafeeq

add this to your log4j.properties

将此添加到您的 log4j.properties

log4j.logger.org.hibernate = WARN

回答by dicho

This worked for me:

这对我有用:

spring.jpa.show-sql=false

回答by Stephane L

Here are three ways (they are probably others) to show or hide hibernate sql queries :

以下是显示或隐藏休眠 sql 查询的三种方法(它们可能是其他方法):

  1. In logger config (level DEBUG for logback) :

    <logger name="org.hibernate.SQL" level="DEBUG" />
    
  2. In Spring config (for instance in java config) :

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    //...
    final Properties jpaProperties = new Properties(); // java.util
    //TODO replace "true" by a variable
    jpaProperties.setProperty("hibernate.show_sql", "true"); 
    lef.setJpaProperties(jpaProperties);
    return lef;
    }
    
  3. In persistence.xml

    <property name="hibernate.show_sql" value="true" />
    
  1. 在记录器配置中(logback 的调试级别):

    <logger name="org.hibernate.SQL" level="DEBUG" />
    
  2. 在 Spring 配置中(例如在 java 配置中):

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    //...
    final Properties jpaProperties = new Properties(); // java.util
    //TODO replace "true" by a variable
    jpaProperties.setProperty("hibernate.show_sql", "true"); 
    lef.setJpaProperties(jpaProperties);
    return lef;
    }
    
  3. 在persistence.xml中

    <property name="hibernate.show_sql" value="true" />
    

It seems that the logger configuration is independant from the persistence configuration. As for the persistence configuration it appears that spring overrides the persistence.xml (I tested it with a persistence.xml loaded by spring, if this is not the case I don't know what will be the behavior).

A difference between the two configurations is the result logging, with logback it will look like this :

似乎记录器配置独立于持久性配置。至于持久性配置,似乎spring覆盖了persistence.xml(我用spring加载的persistence.xml对其进行了测试,如果不是这种情况,我不知道会出现什么行为)。

两种配置之间的区别在于结果日志记录,使用 logback 它将如下所示:

2016-08-25 16:05:39,436 DEBUG org.hibernate.SQL(92) - alter table ...

With the persistence configuration :

使用持久性配置:

Hibernate: alter table ...