https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Interceptor.html says that onPrepareStatement(String sql) is Deprecated. Supply a StatementInspector instead, if you wish to inspect and alter SQL statements.
But I am not clear how I can configure StatementInspector in Hibernate at application level (i don't want to set it at each hibernate session level).
The best way to register a StatementInspector
is to use the hibernate.session_factory.statement_inspector
configuration property.
This way, it does not matter whether you are bootstrapping Hibernate using JPA (e.g. Spring Data JPA) or native Hibernate (e.g. Spring with HibernateTranscationManager
and LocalSessionFactoryBean
).
So, you can to provide the hibernate.session_factory.statement_inspector
via the persistence.xml
JPA configuration file:
<property
name="hibernate.session_factory.statement_inspector"
value="com.vladmihalcea.book.hpjp.hibernate.logging.inspector.SqlCommentStatementInspector"
/>
Or, you can also set the hibernate.session_factory.statement_inspector
programmatically if you're using Spring:
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan({
"com.vladmihalcea.books.high.performance.java.persistence"
});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager
= new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private final Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty(
"hibernate.session_factory.statement_inspector",
SqlCommentStatementInspector.class
);
hibernateProperties.setProperty(
"hibernate.dialect",
"org.hibernate.dialect.H2Dialect"
);
return hibernateProperties;
}
Notice that the hibernate.session_factory.statement_inspector
setting can take either a String
representing he fully-qualified class implementing the StatementInspector
interface, a Class<? extends StatementInspector>
or a StatementInspector
object reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With