Java – JPA with hibernate 5: create entitymanagerfactory programmatically

This problem is specifically about programmatically creating a JPA entitymanagerfactory supported by hibernate 5, which means that there is no configuration XML file instead of spring In addition, this question is specifically about using hibernate interceptor to create entitymanagerfactory

I know how to create a hibernate sessionfactory in the way I want, but I don't want a hibernate sessionfactory. I want a JPA entitymanagerfactory supported by hibernate sessionfactory Given an entitymanagerfactory, there is a way to get the underlying sessionfactory, but if you have sessionfactory and all you want is an entitymanagerfactory wrapper, you seem to be out of luck

Use hibernate version 4.2 2 ejb3configuration has been deprecated, but there seems to be no other way to create entitymanagerfactory programmatically, 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 ejb3cconfiguration was deleted, so I had to use this hack:

EntityManagerFactory buildEntityManagerFactory(
        UnmodifiableMap<String,Interceptor interceptor )
{
    Configuration cfg = new Configuration();
    for( Binding<String,binding.value );
    for( Class<?> annotatedClass : annotatedClasses )
        cfg.addAnnotatedClass( annotatedClass );
    cfg.setInterceptor( interceptor );
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
    ssrb.applySettings( cfg.getProperties() ); //??? why again?
    ServiceRegistry serviceRegistry = ssrb.build();
    return new EntityManagerFactoryImpl( PersistenceUnitTransactionType.RESOURCE_LOCAL,/**/
            /*discardOnClose=*/true,/*sessionInterceptorClass=*/null,/**/
            cfg,serviceRegistry,null );
}

(this is a hacker because I am instantiating entitymanagerfactoryimpl. Containing org.hibernate.jpa.internal.)

Now, using hibernate 5, they have changed the constructor of entitymanagerfactoryimpl, so the above code does not work I can waste hours trying to figure out how to set up so that I can call the constructor, but I'm sure it won't work after several hibernate versions

So this is my question:

Does anyone know a beautiful and clean way to achieve this function

EntityManagerFactory buildEntityManagerFactory( 
        UnmodifiableMap<String,Interceptor interceptor )

Create a hibernate entitymanagerfactory programmatically, which means that instead of using spring, hibernate interceptor is used instead of configuring XML files?

There is an old problem: Hibernate create JPA entitymanagerfactory with out persistence XML, but it has an old version of Hibernate answer, which has been expected in this question This is not possible because I want it to work with hibernate 5. Ideally, it won't use anything discarded or internal so that it can work for a while

Solution

The easiest way is to pass org. Org hibernate. jpa. boot. spi. Persistenceunitdescriptor reference, which is an abstraction of "persistence unit" information In the normal JPA boot, hibernate will be in persistence Build a persistenceunitdescriptor on XML (for JPA's so-called "Se boot") or in javax persistence. spi. Build a persistenceunitdescription on persistenceunitinfo (for what JPA calls "EE boot")

But it's an abstract reason:) you can create your own and pass the hibernate usage you want The expected way of this work is from org hibernate. jpa. boot. spi. Bootstrap start, for example:

EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(
        new CustomPersistenceUnitDescriptor(),Collections.emptyMap()
).build();

...

class CustomPersistenceUnitDescriptor implements PersistenceUnitDescriptor {
    @Override
    public Properties getProperties() {
        final Properties properties = new Properties();
        properties.put( AvailableSettngs.INTERCEPTOR,new MyInterceptor( ... );
        return properties;
    }

    ...
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>