How to disable JPA 2 callback methods and entity listeners
If you want to remove all JPA listeners from Hibernate 4.3.5 (the only one I have tested) it can be done. I will not show how to get hold of the EntityMangerFactory (emf in below code) but after that the below code should be added/run.
Explanation: it seems there is a very central class called org.hibernate.jpa.event.internal.jpa.CallbackRegistryImpl
that contains all registered listeners and callbacks on the entities. By replacing the registry with an empty one no callbacks will be performed.
SessionFactoryImpl sessionFactory = (SessionFactoryImpl) ((EntityManagerFactoryImpl) emf).getSessionFactory();
EventListenerRegistry eventListenerRegistry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
CallbackRegistryImpl emptyRegistry= new CallbackRegistryImpl();
for ( EventType eventType : EventType.values() ) {
final EventListenerGroup eventListenerGroup = eventListenerRegistry.getEventListenerGroup( eventType );
for ( Object listener : eventListenerGroup.listeners() ) {
if ( CallbackRegistryConsumer.class.isInstance( listener ) ) {
( (CallbackRegistryConsumer) listener ).injectCallbackRegistry( emptyRegistry );
}
}
}