Adapted orm.hibernate4 and HibernateJpaVendorAdapter to avoid deprecation warnings on Hibernate 4.3 final
Issue: SPR-11240
This commit is contained in:
@@ -454,7 +454,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
|
||||
if (!definition.isReadOnly() && !txObject.isNewSession()) {
|
||||
// We need AUTO or COMMIT for a non-read-only transaction.
|
||||
FlushMode flushMode = session.getFlushMode();
|
||||
if (FlushMode.isManualFlushMode(session.getFlushMode())) {
|
||||
if (session.getFlushMode().equals(FlushMode.MANUAL)) {
|
||||
session.setFlushMode(FlushMode.AUTO);
|
||||
txObject.getSessionHolder().setPreviousFlushMode(flushMode);
|
||||
}
|
||||
|
||||
@@ -131,11 +131,13 @@ public class LocalSessionFactoryBuilder extends Configuration {
|
||||
* (may be {@code null})
|
||||
* @param resourceLoader the ResourceLoader to load application classes from
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) {
|
||||
getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
|
||||
if (dataSource != null) {
|
||||
getProperties().put(Environment.DATASOURCE, dataSource);
|
||||
}
|
||||
// APP_CLASSLOADER is deprecated as of Hibernate 4.3 but we need to remain compatible with 4.0+
|
||||
getProperties().put(AvailableSettings.APP_CLASSLOADER, resourceLoader.getClassLoader());
|
||||
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -90,7 +90,7 @@ class SpringSessionSynchronization implements TransactionSynchronization, Ordere
|
||||
Session session = getCurrentSession();
|
||||
// Read-write transaction -> flush the Hibernate Session.
|
||||
// Further check: only flush when not FlushMode.MANUAL.
|
||||
if (!FlushMode.isManualFlushMode(session.getFlushMode())) {
|
||||
if (!session.getFlushMode().equals(FlushMode.MANUAL)) {
|
||||
try {
|
||||
SessionFactoryUtils.logger.debug("Flushing Hibernate Session on transaction synchronization");
|
||||
session.flush();
|
||||
|
||||
@@ -68,13 +68,13 @@ import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.orm.jpa.JpaDialect} implementation for
|
||||
* Hibernate EntityManager. Developed against Hibernate 3.6 and 4.2.
|
||||
* Hibernate EntityManager. Developed against Hibernate 3.6 and 4.2/4.3.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.0
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
@SuppressWarnings({"serial", "deprecation"})
|
||||
public class HibernateJpaDialect extends DefaultJpaDialect {
|
||||
|
||||
private static Class<?> optimisticLockExceptionClass;
|
||||
@@ -88,6 +88,7 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
|
||||
optimisticLockExceptionClass = cl.loadClass("org.hibernate.dialect.lock.OptimisticEntityLockException");
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// OptimisticLockException is deprecated on Hibernate 4.x; we're just using it on 3.x anyway
|
||||
optimisticLockExceptionClass = OptimisticLockException.class;
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,7 +18,6 @@ package org.springframework.orm.jpa.vendor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceProvider;
|
||||
@@ -36,27 +35,64 @@ import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.ejb.HibernateEntityManager;
|
||||
import org.hibernate.ejb.HibernateEntityManagerFactory;
|
||||
import org.hibernate.ejb.HibernatePersistence;
|
||||
|
||||
import org.springframework.orm.jpa.JpaDialect;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.orm.jpa.JpaVendorAdapter} implementation for
|
||||
* Hibernate EntityManager. Developed and tested against Hibernate 3.6 and 4.2.
|
||||
* Hibernate EntityManager. Developed and tested against Hibernate 3.6 and 4.2/4.3.
|
||||
*
|
||||
* <p>Exposes Hibernate's persistence provider and EntityManager extension interface,
|
||||
* and supports {@link AbstractJpaVendorAdapter}'s common configuration settings.
|
||||
*
|
||||
* <p>Note that the package location of Hibernate's JPA support changed from 4.2 to 4.3:
|
||||
* from {@code org.hibernate.ejb.HibernateEntityManager(Factory)} to
|
||||
* {@code org.hibernate.jpa.HibernateEntityManager(Factory)}. As of Spring 4.0,
|
||||
* we're exposing the correct, non-deprecated variant depending on the Hibernate
|
||||
* version encountered at runtime, in order to avoid deprecation log entries.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rod Johnson
|
||||
* @since 2.0
|
||||
* @see org.hibernate.ejb.HibernatePersistence
|
||||
* @see org.hibernate.ejb.HibernateEntityManager
|
||||
*/
|
||||
public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
|
||||
|
||||
private final PersistenceProvider persistenceProvider = new HibernatePersistence();
|
||||
private final PersistenceProvider persistenceProvider;
|
||||
|
||||
private final JpaDialect jpaDialect = new HibernateJpaDialect();
|
||||
|
||||
private final Class<? extends EntityManagerFactory> entityManagerFactoryInterface;
|
||||
|
||||
private final Class<? extends EntityManager> entityManagerInterface;
|
||||
|
||||
|
||||
@SuppressWarnings({"deprecation", "unchecked"})
|
||||
public HibernateJpaVendorAdapter() {
|
||||
PersistenceProvider providerToUse;
|
||||
Class<? extends EntityManagerFactory> emfIfcToUse;
|
||||
Class<? extends EntityManager> emIfcToUse;
|
||||
try {
|
||||
// Try Hibernate 4.3's org.hibernate.jpa package in order to avoid deprecation warnings
|
||||
ClassLoader cl = HibernateJpaVendorAdapter.class.getClassLoader();
|
||||
Class<?> hibernatePersistenceProviderClass = cl.loadClass("org.hibernate.jpa.HibernatePersistenceProvider");
|
||||
providerToUse = (PersistenceProvider) hibernatePersistenceProviderClass.newInstance();
|
||||
emfIfcToUse = (Class<? extends EntityManagerFactory>) cl.loadClass("org.hibernate.jpa.HibernateEntityManagerFactory");
|
||||
emIfcToUse = (Class<? extends EntityManager>) cl.loadClass("org.hibernate.jpa.HibernateEntityManager");
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// Fall back to Hibernate 3.6-4.2 org.hibernate.ejb package
|
||||
providerToUse = new HibernatePersistence();
|
||||
emfIfcToUse = HibernateEntityManagerFactory.class;
|
||||
emIfcToUse = HibernateEntityManager.class;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Found HibernatePersistenceProvider but could not instantiate it", ex);
|
||||
}
|
||||
this.persistenceProvider = providerToUse;
|
||||
this.entityManagerFactoryInterface = emfIfcToUse;
|
||||
this.entityManagerInterface = emIfcToUse;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PersistenceProvider getPersistenceProvider() {
|
||||
@@ -101,15 +137,15 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
|
||||
protected Class<?> determineDatabaseDialectClass(Database database) {
|
||||
switch (database) {
|
||||
case DB2: return DB2Dialect.class;
|
||||
case DERBY: return DerbyDialect.class;
|
||||
case DERBY: return DerbyDialect.class; // DerbyDialect deprecated in 4.x
|
||||
case H2: return H2Dialect.class;
|
||||
case HSQL: return HSQLDialect.class;
|
||||
case INFORMIX: return InformixDialect.class;
|
||||
case MYSQL: return MySQLDialect.class;
|
||||
case ORACLE: return Oracle9iDialect.class;
|
||||
case POSTGRESQL: return PostgreSQLDialect.class;
|
||||
case POSTGRESQL: return PostgreSQLDialect.class; // PostgreSQLDialect deprecated in 4.x
|
||||
case SQL_SERVER: return SQLServerDialect.class;
|
||||
case SYBASE: return org.hibernate.dialect.SybaseDialect.class;
|
||||
case SYBASE: return org.hibernate.dialect.SybaseDialect.class; // SybaseDialect deprecated in 3.6 but not 4.x
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
@@ -121,12 +157,12 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
|
||||
|
||||
@Override
|
||||
public Class<? extends EntityManagerFactory> getEntityManagerFactoryInterface() {
|
||||
return HibernateEntityManagerFactory.class;
|
||||
return this.entityManagerFactoryInterface;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends EntityManager> getEntityManagerInterface() {
|
||||
return HibernateEntityManager.class;
|
||||
return this.entityManagerInterface;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user