-->
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.  [ 1 post ] 
Author Message
 Post subject: How to use EJB3 with a custom Hibernate interceptor instance
PostPosted: Mon Dec 17, 2007 7:54 am 
Newbie

Joined: Mon Dec 17, 2007 7:25 am
Posts: 1
I need to use a custom Hibernate Interceptor in an EJB3 environment, and by custom, I mean an Interceptor I have instantiated myself as it needs to be linked to other application objects (this means declaring the interceptor type an let Hibernate instantiate it is not a solution here). I actually use EntityManager 3.3.1 associated with Spring 2.5.
With plain Hibernate, I would have the method :
Code:
sessionFactory.openSession(myInterceptor)
but no such thing exists neither with the EntityManager nor with the HibernateEntityManager.
When I look at the code of EntityManagerImpl, there is a getRawSession() method that doesn't give a chance to push an interceptor.
In my case, even an "EntityManagerFactory" scoped would be sufficient, but here again, there is no way to add my own interceptor and the code of EntityManagerFactoryImpl shows a nice comment ensuring I am not wrong:
Code:
//TODO support discardOnClose, persistencecontexttype?, interceptor

A third thing I tried is to use a custom implementation of HibernatePersistence that injects my interceptor (taken from a Map) in the SessionFactory this way
Code:
public class MyCustomPersistence extends HibernatePersistence
{
  ...
  public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
    Ejb3Configuration cfg = new Ejb3Configuration();
    Ejb3Configuration configured = cfg.configure( info, map );
    Interceptor interceptor = (Interceptor) overridenProperties.get("custom.interceptor");
    if (interceptor != null)
    {
      configured.setInterceptor(interceptor);
    }
    return configured != null ? configured.buildEntityManagerFactory() : null;
  }

With this solution, I can inject my Interceptor in the map using Spring, and have it configured in the SessionFactory. But this won't work because the Ejb3Configuration class explicitly checks the type of the provider as being exactly "org.hibernate.ejb.HibernatePersistence" (and does not even allow a derivative class), and thus refuses to use mine.
Well, in the end, I don't want to rewrite Ejb3Configuration which is too complex to maintain.
Has anybody had this problem and found a clean solution (Ok, I could use some statics to pass them to my Interceptor and have it instantiated by Hibernate, but as this will go in a quite big production application, I want to avoid these dirty kind of things).
Thanks


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

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.