Java JPA 2.1/Hibernate 4.3 弃用警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23041964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA 2.1/Hibernate 4.3 deprecation warning
提问by Ashot Karakhanyan
I'm using JPA 2.1
sample application with Hibernate 4.3.x
implementation.
我正在使用JPA 2.1
带有Hibernate 4.3.x
实现的示例应用程序。
<?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.
在pom.xml
我有以下依赖项。
<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?
那么,这是我错误配置的问题(我可以避免吗?),还是 Hibernate 实现中的问题?
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();
}
}
回答by Shaka Shadows
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 EntityManagerFactoryto use this class. I have removed the warning with the following configuration (notice I've specified a bean for jpaVendorAdapter):
对于遇到这个问题的 Spring 人员,这里是正在发生的事情:即使 Spring 人员使用HibernateJpaVendorAdapter类解决了这个问题,你实际上需要告诉你的JPA EntityManagerFactory使用这个类。我已经使用以下配置删除了警告(注意我已经为jpaVendorAdapter指定了一个 bean ):
<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 Persistenceclass. This is the actual code of the createEntityManagerFactorywhich is invoked when you call it by passing just the persistence unit name:
我已经调试了您的代码,似乎已弃用的类是由Persistence类自动加载的。这是createEntityManagerFactory的实际代码,当您通过传递持久性单元名称调用它时调用它:
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.HibernatePersistenceby default as the first possible provider (as a fallback solution I guess). What I would do is something like this:
所述getProviders()方法包括org.hibernate.ejb.HibernatePersistence默认为第一可能的提供者(作为备用溶液I猜)。我会做的是这样的:
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 Persistenceclass by this CustomPersistence. I hope this helps.
我已经测试了该代码,它似乎足以解决问题。在您的代码中,您只需要用这个CustomPersistence替换Persistence类。我希望这有帮助。
回答by 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.
通过遵循弃用警告中的建议,我发现可以通过实体管理器工厂的相对整洁的 Spring bean 创建来克服这个问题。
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:
我们可以看到从HibernatePersistenceProvider javadocs 中获取与 javax.persistence.EntityManagerFactory 兼容的类的路径。首先你需要一个 HibernatePersistenceProvider 的实例,然后调用 createEntityManagerFactory 方法。这是避免弃用警告的 Spring Bean 接线:
<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 hereshowing the maven dependencies.
这里有一个Stackoverflow 线程显示了 Maven 依赖项。
回答by stephanmunich
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
该问题似乎已作为 hibernate 中的错误提交,并已针对 5.0.0.Beta1 版修复,请参阅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>