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