Yes, you can intercept it like this:
(1) Intercept hibernate API
Code:
public static SessionFactory wrapSessionFactory(final SessionFactory sessionFactory) {
return (SessionFactory)Proxy.newInstacne(
SessionFactory.class.getClassLoader(),
sessionFactory.getClass().getInterfaces(),
new InvocationHandler(Object proxy, Method method, Object[] args) throws Throwable {
Object retval = method.invoke(sessionFactory, args);
if (retval instanceof Session && method.getName.equals("openSession") {
Session session = (Session)retval;
session.setFlushMode(FlushMode.COMMIT);
}
return retval;
}
);
}
(2) Intercept JPA
Code:
public static EntityManagerFactory wrapEntityManagerFactory(final EntiyManagerFactory entityManagerFactory) {
return (EntityManagerFactory)Proxy.newInstacne(
EntiyManagerFactory.class.getClassLoader(),
entityManagerFactory.getClass().getInterfaces(),
new InvocationHandler(Object proxy, Method method, Object[] args) throws Throwable {
Object retval = method.invoke(entityManagerFactory, args);
if (retval instanceof EntiyManager && method.getName.equals("createEntityManager") {
EntityManager entityManager = (EntityManager)retval;
entityManager.unwrap(Session.class).setFlushMode(FlushMode.COMMIT);
}
return retval;
}
);
}