JPA 2.1/Hibernate 4.3 deprecation warning

有些话、适合烂在心里 提交于 2019-11-29 13:45:36

For Spring folks having that issue, here is what's happening: even when Spring guys solved the issue with class HibernateJpaVendorAdapter, you actually need to tell to your JPA EntityManagerFactory to use this class. I have removed the warning with the following configuration (notice I've specified a bean for jpaVendorAdapter):

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="goal-control-unit" />
    <property name="jpaDialect">
        <bean class="cl.antica.goalcontrol.util.ExtendedHibernateJPADialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property> 
</bean>

I have debugged your code and it seems that deprecated class is loaded automatically by Persistence class. This is the actual code of the createEntityManagerFactory which is invoked when you call it by passing just the persistence unit name:

public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
    EntityManagerFactory emf = null;
    List<PersistenceProvider> providers = getProviders();
    for ( PersistenceProvider provider : providers ) {
        emf = provider.createEntityManagerFactory( persistenceUnitName, properties );
        if ( emf != null ) {
            break;
        }
    }
    if ( emf == null ) {
        throw new PersistenceException( "No Persistence provider for EntityManager named " + persistenceUnitName );
    }
    return emf;
}

The getProviders() method includes org.hibernate.ejb.HibernatePersistence by default as the first possible provider (as a fallback solution I guess). What I would do is something like this:

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolverHolder;

import org.hibernate.ejb.HibernatePersistence;

@SuppressWarnings({"deprecation", "rawtypes"})
public class CustomPersistence extends Persistence {

    public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) {
        return CustomPersistence.createEntityManagerFactory(persistenceUnitName, null);
    }

    public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
        EntityManagerFactory emf = null;
        List<PersistenceProvider> providers = getProviders();
        PersistenceProvider defaultProvider = null;
        for (PersistenceProvider provider : providers) {
            if (provider instanceof HibernatePersistence) {
                defaultProvider = provider;
                continue;
            }
            emf = provider.createEntityManagerFactory(persistenceUnitName, properties);
            if (emf != null) {
                break;
            }
        }
        if (emf == null && defaultProvider != null)
            emf = defaultProvider.createEntityManagerFactory( persistenceUnitName, properties );
        if ( emf == null ) {
            throw new PersistenceException( "No Persistence provider for EntityManager named " + persistenceUnitName );
        }
        return emf;
    }

    protected static List<PersistenceProvider> getProviders() {
        return PersistenceProviderResolverHolder
                .getPersistenceProviderResolver()
                .getPersistenceProviders();
    }

}

I have tested that code and it seems to be good enough to solve the issue. In your code, you just need to replace Persistence class by this CustomPersistence. I hope this helps.

The problem seems to have been filed as a bug in hibernate and is fixed for version 5.0.0.Beta1, see https://hibernate.atlassian.net/browse/HHH-9141

At least I had the same problem and changing the dependency to this version made my warnings go away:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
antonycc

By following the advice in the deprecation warning, I have found it possible to overcome this problem with a relatively tidy Spring bean creation of the Entity Manager Factory.

Starting here:

WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.

We can see the path to obtain a class compatible with javax.persistence.EntityManagerFactory from the HibernatePersistenceProvider javadocs. First you need an instance of the HibernatePersistenceProvider, then the createEntityManagerFactory method is called. Here is the Spring Bean wiring which avoids the deprecation warning:

<bean id="persistenceProvider" class="org.hibernate.jpa.HibernatePersistenceProvider">
</bean>
<bean id="emf" factory-bean="persistenceProvider" factory-method="createEntityManagerFactory">
   <constructor-arg value="testpersistence"/>
   <constructor-arg><map/></constructor-arg>
</bean>

There is a Stackoverflow thread here showing the maven dependencies.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!