javaconfig中的Hibernate(“showSql”,“true”)不起作用

时间:2015-10-07 19:53:16

标签: java spring hibernate jpa

我将JPA与Hibernate一起用作持久性提供程序,并使用以下配置类进行配置。 问题是我从未看到Hibernate编译的SQL语句,尽管我在其他属性中有showSql = true。请检查下面。现在它是properties.setProperty("hibernate.showsql", "true");,但我也尝试properties.setProperty("showSql", "true");没有效果。 我没有persistence.xml,我不使用root-context.xml。 我的servlet-context.xml只包含

<mvc:annotation-driven />

    <beans:bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <task:annotation-driven />

另外,我的meta-inf / log4j.xml包含INFO级别的所有记录器,包括

<logger name="org.hibernate.SQL">
    <level value="info" />
</logger>
我添加的

也没有任何影响。 我做错了什么?

    @Configuration
@EnableTransactionManagement
@ComponentScan("com")
public class MySQLconfiguration {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] { "com" });

     em.setPersistenceProviderClass(HibernatePersistenceProvider.class);
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalProperties());
          return em;
       }


    @Bean(destroyMethod = "close")
    public DataSource dataSource(){
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/test"); 
        hikariConfig.setUsername("user");
        hikariConfig.setPassword("user");
        hikariConfig.setMaximumPoolSize(5);
        hikariConfig.setMaxLifetime(30000);
        hikariConfig.setIdleTimeout(30000);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setPoolName("springHikariCP");

        hikariConfig.addDataSourceProperty("dataSource.cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("dataSource.prepStmtCacheSqlLimit", "2048");
        hikariConfig.addDataSourceProperty("dataSource.useServerPrepStmts", "true");
        HikariDataSource dataSource = new HikariDataSource(hikariConfig);
        return dataSource;
    }


     @Bean
       public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
          return transactionManager;
       }

     @Bean
       public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
          return new PersistenceExceptionTranslationPostProcessor();
       }

       Properties additionalProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", "update");
          properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
          properties.setProperty("hibernate.archive.autodetection", "class");
          properties.setProperty("hibernate.showSql", "true");
          return properties;
       }

}

1 个答案:

答案 0 :(得分:6)

属性键应为hibernate.show_sql,而不是hibernate.showSql

要验证,您可以查看Hibernate类AvailableSettings,其中列出了可用的配置选项,其中之一是:

/**
 * Enable logging of generated SQL to the console
 */
String SHOW_SQL = "hibernate.show_sql";