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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…