-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to install a global Interceptor with Hibernate 5
PostPosted: Tue Jul 18, 2017 9:53 am 
Newbie

Joined: Tue Jul 18, 2017 9:45 am
Posts: 5
We are using Hibernate with a Wildfly application server and are currently migrating from Wildfly 8.2.0 (Hibernate 4.3) to 10.1.0 (Hibernate 5.0).

With Hibernate 4.3 we could install a global Interceptor for all Sessions using a Hibernate Integrator.

Code:
public class MyIntegrator implements Integrator {
    private static Logger logger = Logger.getLogger(MyIntegrator.class);

    @Override
    public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
        logger.info("Install GlobalInterceptor in " + configuration.getProperty("hibernate.session_factory_name"));
        configuration.setInterceptor(new GlobalInterceptor());
    }

    ...
}


This does not work anymore with Hibernate 5.0 since the API of Integrator has changed.
Instead of the Configuration Hibernate now passes a Metadata instance into integrate.

Code:
@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
        // TODO Set the Interceptor for all Sessions ... but how?
}


How can I set an Interceptor with the API of Metadata?
I couldn't find any information on this in the migration guides.

Or are there other ways the configure Hibernate to use an interceptor?

Thank you for any hints!
Christoph


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Tue Jul 18, 2017 11:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It's very simple. Try it like this:

Code:
final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
sessionFactoryBuilder.applyInterceptor( new GlobalInterceptor() );


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Wed Jul 19, 2017 5:46 am 
Newbie

Joined: Tue Jul 18, 2017 9:45 am
Posts: 5
vlad wrote:
It's very simple. Try it like this:

final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
sessionFactoryBuilder.applyInterceptor( new GlobalInterceptor() );


Thanks! I tried this, but the Interceptor is not used in the sessions that are created for the application server.
(We use the standard JPA javax.persistence.EntityManager in the buisiness logic.)

We are using Widlfly 10.1.0 with Hibernate 5.0.10.


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Wed Jul 19, 2017 5:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's strange because that's what Configuration does too.

You might want to open a ticket, but, chances are, it will be fixed in 5.2 and it might not get into 5.0 branch. So, try to replicate it with our test case templates, and open a Jira issue for it.


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Thu Jul 20, 2017 5:12 am 
Newbie

Joined: Tue Jul 18, 2017 9:45 am
Posts: 5
vlad wrote:
You might want to open a ticket, but, chances are, it will be fixed in 5.2 and it might not get into 5.0 branch. So, try to replicate it with our test case templates, and open a Jira issue for it.

The templates are great. I was able to quickly verify the bug. I opend a jira issue here:
https://hibernate.atlassian.net/browse/HHH-11880

Are you aware of another hook I could use to install an interceptor into a session?
A good time would be when a JPA EntityManager is created and opens the Hibernate Session it will use.


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Thu Jul 20, 2017 5:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Thanks. Check out this article which shows you a way to configure the Integrator via the hibernate.integrator_provider configuration property.


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Thu Jul 20, 2017 8:27 am 
Newbie

Joined: Tue Jul 18, 2017 9:45 am
Posts: 5
vlad wrote:
Thanks. Check out this article which shows you a way to configure the Integrator via the hibernate.integrator_provider configuration property.


Thanks. Do you know of a documentation describing the different event types? I couldn't find much detail to specific events in the Hibernate User Guide [1].
I would be interessted to get notified when transactions begin and end (commit as well as rollback). In our use case we want to collect changes to entities and process these after the transaction has been committed.

[1]: https://docs.jboss.org/hibernate/orm/5. ... nts-events


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Thu Jul 20, 2017 9:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The event system does not cover transaction management. The Interceptor provides you callbacks for that:

Code:
/**
* Called when a Hibernate transaction is begun via the Hibernate <tt>Transaction</tt>
* API. Will not be called if transactions are being controlled via some other
* mechanism (CMT, for example).
*
* @param tx The Hibernate transaction facade object
*/
void afterTransactionBegin(Transaction tx);

/**
* Called before a transaction is committed (but not before rollback).
*
* @param tx The Hibernate transaction facade object
*/
void beforeTransactionCompletion(Transaction tx);

/**
* Called after a transaction is committed or rolled back.
*
* @param tx The Hibernate transaction facade object
*/
void afterTransactionCompletion(Transaction tx);


Top
 Profile  
 
 Post subject: Re: How to install a global Interceptor with Hibernate 5
PostPosted: Mon Jul 24, 2017 3:59 am 
Newbie

Joined: Tue Jul 18, 2017 9:45 am
Posts: 5
vlad wrote:
The event system does not cover transaction management. The Interceptor provides you callbacks for that:


This was my impression as well. Thanks for confirming this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.