This question is about programmatically creating a JPA EntityManagerFactory supported by Hibernate 5, which means without xml configuration files and without using Spring . Also, this question is specifically about creating an EntityManagerFactory with a Hibernate interceptor .
I know how to create a Hibernate SessionFactory way I want, but I don't need a Hibernate SessionFactory , I want a JPA EntityManagerFactory supported by a Hibernate SessionFactory . Given EntityManagerFactory , there is a way to get a basic SessionFactory , but if you have a SessionFactory and all you need is an EntityManagerFactory wrapper around it, it seems like you're out of luck.
With Hibernate 4.2.2, Ejb3Configuration already deprecated, but there seems to be no other way to programmatically create an EntityManagerFactory , so I did something like this:
@SuppressWarnings( "deprecation" ) EntityManagerFactory buildEntityManagerFactory( UnmodifiableMap<String,String> properties, UnmodifiableCollection<Class<?>> annotatedClasses, Interceptor interceptor ) { Ejb3Configuration cfg = new Ejb3Configuration(); for( Binding<String,String> binding : properties ) cfg.setProperty( binding.key, binding.value ); for( Class<?> annotatedClass : annotatedClasses ) cfg.addAnnotatedClass( annotatedClass ); cfg.setInterceptor( interceptor ); return cfg.buildEntityManagerFactory(); }
With Hibernate 4.3.0, Ejb3Configuration been removed, so I had to use this hack:
EntityManagerFactory buildEntityManagerFactory( UnmodifiableMap<String,String> properties, UnmodifiableCollection<Class<?>> annotatedClasses, Interceptor interceptor ) { Configuration cfg = new Configuration(); for( Binding<String,String> binding : properties ) cfg.setProperty( binding.key, binding.value ); for( Class<?> annotatedClass : annotatedClasses ) cfg.addAnnotatedClass( annotatedClass ); cfg.setInterceptor( interceptor ); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); ssrb.applySettings( cfg.getProperties() );
(This is a hack because I am creating an instance of EntityManagerFactoryImpl , which is located in the package org.hibernate.jpa.internal .)
Now, with Hibernate 5, they have changed the constructor of EntityManagerFactoryImpl , so the code above does not work. I can spend several hours figuring out how to set things up so that I can call this constructor, but I am sure that after a couple of versions of Hibernate this will not work either.
So this is my question:
Does anyone know of a nice and clean way to implement this feature
EntityManagerFactory buildEntityManagerFactory( UnmodifiableMap<String,String> properties, UnmodifiableCollection<Class<?>> annotatedClasses, Interceptor interceptor )
to create a Hibernate EntityManagerFactory programmatically , which means without xml configuration files and without using Spring , but with a Hibernate interceptor ?
There is this old question: Hibernate creates a JPA EntityManagerFactory using persistence.xml , but it has an answer for an older version of Hibernate that was already foreseen in this question. This will not happen, because I want it to work with Hibernate 5, and ideally, so as not to use anything outdated or internal, in order to have some chances of working for a long time.