2015-10-07 70 views
0

我使用JPA和Hibernate作爲持久性提供程序,並使用下面的配置類對其進行配置。 問題是,我從來沒有看到由Hibernate編譯的SQL語句,雖然我在其他屬性中有showSql = true。請檢查下面。現在它是properties.setProperty("hibernate.showsql", "true");,但我也試過properties.setProperty("showSql", "true");沒有效果。 我沒有persistence.xml,也沒有使用root-context.xml。 我的servlet-context.xml中只包含javaconfig中的Hibernate(「showSql」,「true」)不起作用

<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

'hibernate.show_sql' ... – Reimeus

回答

5

屬性鍵應該是hibernate.show_sql,不hibernate.showSql


要進行驗證,你可以看看休眠AvailableSettings類,其中列出了可用的配置選項,其中之一是:

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

謝謝你我用google搜索了這個屬性應該如何在javaconfig中使用,但沒有找到它,所以決定它應該和xml一樣 –

相關問題