-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How I can configure StatementInspector in Hibernate?
PostPosted: Tue Aug 23, 2016 7:51 pm 
Newbie

Joined: Sun Aug 21, 2016 8:14 am
Posts: 2
https://docs.jboss.org/hibernate/orm/5. ... eptor.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).


Top
 Profile  
 
 Post subject: Re: How I can configure StatementInspector in Hibernate?
PostPosted: Mon Aug 29, 2016 5:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Assuming you have a MyStatementInspectorwhich implements StatementInspector.

You can supply it at SessionFactory level:

Code:
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
        .configure( "org/hibernate/example/hibernate.cfg.xml" )
        .build();

Metadata metadata = new MetadataSources( standardRegistry )
    .addAnnotatedClass( MyEntity.class )
    .addAnnotatedClassName( "org.hibernate.example.Customer" )
    .addResource( "org/hibernate/example/Order.hbm.xml" )
    .addResource( "org/hibernate/example/Product.orm.xml" )
    .getMetadataBuilder()
    .applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
    .build();

SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();

// Supply an StatementInterceptor
sessionFactoryBuilder.applyStatementInspector( new MyStatementInspector() );

SessionFactory sessionFactory = sessionFactoryBuilder.build();


Or, as I explained in this article, you could 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:

Code:
<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:

Code:
@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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.