我正在使用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>
在pom.xml
我有以下依赖。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
示例命令行应用程序正常工作(非常简单),但启动时会收到以下警告消息。
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.
那么,这是我的错误配置问题(我可以避免它吗?),或者它是Hibernate实现中的问题?
已更新
以下是我使用的代码:
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();
}
}
答案 0 :(得分:6)
对于有这个问题的Spring人来说,这就是正在发生的事情:即使Spring的人解决了类 HibernateJpaVendorAdapter 的问题,你实际上需要告诉你的 JPA EntityManagerFactory < / strong>使用此课程。我已使用以下配置删除了警告(请注意我已为 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>
我已调试了您的代码,似乎已弃用的类由持久性类自动加载。这是 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;
}
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) {
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();
}
}
我已经测试了该代码,它似乎足以解决问题。在您的代码中,您只需要通过此 CustomPersistence 替换持久性类。我希望这会有所帮助。
答案 1 :(得分:1)
这个问题似乎是作为hibernate中的一个错误提交的,并且已针对5.0.0.Beta1修复,请参阅https://hibernate.atlassian.net/browse/HHH-9141
至少我遇到了同样的问题,并且将依赖项更改为此版本会使我的警告消失:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.0.Beta1</version>
</dependency>
答案 2 :(得分:0)
通过遵循弃用警告中的建议,我发现可以通过相对整齐的Spring bean创建Entity Manager Factory来解决这个问题。
从这里开始:
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
我们可以看到从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>
有Stackoverflow thread here显示maven依赖项。