JPA 2.1 / Hibernate 4.3弃用警告 [英] JPA 2.1/Hibernate 4.3 deprecation warning

查看:127
本文介绍了JPA 2.1 / Hibernate 4.3弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Hibernate 4.3.x 实现中使用 JPA 2.1 示例应用程序。

 <?xml version =1.0encoding =UTF-8?> 
< persistence version =2.1xmlns =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>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost:3306 / common/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.usernamevalue =root/>
< property name =hibernate.connection.passwordvalue =root/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =true/>
< / properties>

< / persistence-unit>
< /余辉>

pom.xml 我有以下

 <依赖关系> 
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 4.3.5.Final< / version>
< /依赖关系>

示例命令行应用程序正常工作(t非常简单),但在出现以下警告消息时我开始它。

  2014年4月13日下午1:12:43 org.hibernate.ejb.HibernatePersistence logDeprecation 
警告:HHH015016:遇到不推荐使用的javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence];改为使用[org.hibernate.jpa.HibernatePersistenceProvider]。

所以,问题是我错误的配置(我可以避免它吗?),或者它是Hibernate实现中的一个问题?



已更新



以下是我使用的代码:

  import net.roseindia.model.Product; 

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

public class AppTest {

private static final String PERSISTENCE_UNIT_NAME =unit1;
私有静态EntityManagerFactory工厂;

public class AppTest {

private static final String PERSISTENCE_UNIT_NAME =unit1;
私有静态EntityManagerFactory工厂;

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

em.getTransaction()。begin();

产品产品=新产品();
product.setProductName(JPA 2.1 Book);
product.setProductDescription(这是关于JPA 2.1的最新书籍);
product.setStockQty(100.00);
product.setPrice(95.99);
em.persist(product);
em.getTransaction()。commit();
em.close();
factory.close();


$ b


解决方案

对于有这个问题的Spring人来说,这里发生了什么:即使Spring家伙用类 HibernateJpaVendorAdapter 解决了这个问题,你实际上需要告诉你的 JPA EntityManagerFactory 使用这个类。我已经通过以下配置删除了警告(注意,我为 jpaVendorAdapter 指定了一个bean):

 < bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalEntityManagerFactoryBean> 
< property name =persistenceUnitNamevalue =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>

我已经调试了你的代码,看起来不赞成的类是由 Persistence <强类。这是 createEntityManagerFactory 的实际代码,当您仅通过传递持久性单元名称来调用 时,将调用

c $ c> public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName,Map properties){
EntityManagerFactory emf = null;
列出< PersistenceProvider> providers = getProviders(); (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);
}
返回emf;
}

getProviders()方法包括 org.hibernate.ejb.HibernatePersistence 作为第一个可能的提供者(作为我猜想的后备解决方案)。我会做的是这样的:

  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){
返回CustomPersistence.createEntityManagerFactory(persistenceUnitName,null);


public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName,Map properties){
EntityManagerFactory emf = null;
列出< PersistenceProvider> providers = getProviders();
PersistenceProvider defaultProvider = null; (PersistenceProvider provider:providers)
{
if(provider instanceof HibernatePersistence){
defaultProvider = provider;
继续;
}
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);
}
返回emf;
}

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

}

我测试过这个代码,它似乎足以解决这个问题。在你的代码中,你只需要用 CustomPersistence 替换 Persistence 类。我希望这有助于。


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();

    }
}

解决方案

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.

这篇关于JPA 2.1 / Hibernate 4.3弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆