Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA 2.1/Hibernate 4.3 deprecation warning

I'm using JPA 2.1 sample application with Hibernate 4.3.x implementation.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                                http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="unit1">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>net.roseindia.model.Product</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/common"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>

    </persistence-unit>
</persistence>

In pom.xml I have the following dependency.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.5.Final</version>
</dependency>

The sample command line application works normally (t is very simple) but I get the following warning message when I start it.

Apr 13, 2014 1:12:43 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.

So, Is it the problem my wrong configuration (and can I avoid it?), or it is an issue in Hibernate implementation?

UPDATED

Here is the code which I use:

import net.roseindia.model.Product;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class AppTest {

private static final String PERSISTENCE_UNIT_NAME = "unit1";
private static EntityManagerFactory factory;

public class AppTest {

    private static final String PERSISTENCE_UNIT_NAME = "unit1";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();

        em.getTransaction().begin();

        Product product = new Product();
        product.setProductName("JPA 2.1 Book");
        product.setProductDescription("This is the latest book on JPA 2.1");
        product.setStockQty(100.00);
        product.setPrice(95.99);
        em.persist(product);
        em.getTransaction().commit();
        em.close();
        factory.close();

    }
}
like image 532
Ashot Karakhanyan Avatar asked Apr 13 '14 11:04

Ashot Karakhanyan


2 Answers

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.

like image 169
Shaka Shadows Avatar answered Nov 20 '22 11:11

Shaka Shadows


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.

like image 1
antonycc Avatar answered Nov 20 '22 09:11

antonycc