我有一个基本的 Spring 4 JPA 应用程序。我使用所有 Java 配置(根本没有 XML)。我想关闭控制台调试消息。我看到了很多关于此的问题并尝试了解决方案,但我仍然看到了所有消息。
控制台消息如下所示:
14:58:29.301 [main] DEBUG o.h.loader.entity.plan.EntityLoader....
14:58:29.328 [main] INFO o.h.tool.hbm2ddl.SchemaUpdate....
....
14:58:29.905 [main] DEBUG o.h.h.i.ast.QueryTranslatorImpl - --- HQL AST ---
\-[QUERY] Node: 'query'
\-[SELECT_FROM] Node: 'SELECT_FROM'
+-[FROM] Node: 'from'
| \-[RANGE] Node: 'RANGE'
| +-[DOT] Node: '.'
| | +-[IDENT] Node: 'hello'
| | \-[IDENT] Node: 'Customer'
| \-[ALIAS] Node: 'generatedAlias0'
\-[SELECT] Node: 'select'
\-[IDENT] Node: 'generatedAlias0'
....
Hundreds of lines more....
我尝试在 HibernateJpaVendorAdapter 和 LocalContainerEntityManagerFactoryBean 中设置 show_sql ,如下所示:
@Bean
public EntityManagerFactory entityManagerFactory() throws SQLException {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
LocalContainerEntityManagerFactoryBean factory = new
LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("hello");
factory.setDataSource(dataSource());
Properties jpaProperties = new Properties();
jpaProperties.put( "hibernate.dialect", "org.hibernate.dialect.MySQLDialect" );
jpaProperties.put( "hibernate.show_sql", false );
jpaProperties.put( "show_sql", false );
jpaProperties.put( "hibernate.generate_statistics", false );
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
return factory.getObject();
}
感谢您对此的任何想法!
-- 编辑 -- 更多信息
我所做的是使用 Spring Tools Suite 创建一个 Spring Starter 项目,然后选择 JPA。然后我将 MySQL 添加到我的 pom.xml 中。
作为测试,我有一个基本的 Customer 和 CustomerRepository 类以及上面提到的 JPA 配置。
我的应用程序类:
@Configuration
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
AbstractApplicationContext context = new
AnnotationConfigApplicationContext(JPAConfiguration.class);
CustomerRepository repository = context.getBean(CustomerRepository.class);
//use the repository.....
((AbstractApplicationContext) context).close();
context.close();
}
}
这就是它——一个使用 Spring Tools Suite 创建的非常基本的 JPA Spring Starter 项目。如果我能弄清楚如何处理登录,我可以将该信息转化为我的实际项目。
- 编辑 - 甚至更多信息 - 它已修复!
好吧,这很有趣......
我更改了我的应用程序类,问题就消失了。
因此,使用这个应用程序(相对于上面发布的应用程序)并且日志记录问题现在可以了——任何人都可以评论为什么它会这样工作?
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(Application.class);
CustomerRepository repository = context.getBean(CustomerRepository.class);
//use the repository.....
context.close();
}
}
请注意,无论我如何执行应用程序类,艾伦·海伊下面提供的解决方案也都非常有效!
请注意,无论哪种方式,您仍然可以设置 setJpaProperties,如我的配置所示(请参阅上面的 Bean)来控制是否要查看 Hibernate 的 SQL 等。