Use custom condition to detect Hibernate 4.2 and 4.3

...without any deprecation warnings and with some chance that it will
still work with Hibernate 5.

Fixes gh-256
pull/276/head
Dave Syer 11 years ago
parent 258a1115be
commit 6528062a49

@ -21,22 +21,28 @@ import java.util.Map;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.hibernate.jpa.HibernateEntityManager;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.HibernateEntityManagerCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.orm.jpa.SpringNamingStrategy; import org.springframework.boot.orm.jpa.SpringNamingStrategy;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ClassUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Hibernate JPA. * {@link EnableAutoConfiguration Auto-configuration} for Hibernate JPA.
@ -45,8 +51,8 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
*/ */
@Configuration @Configuration
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, @ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class,
EnableTransactionManagement.class, EntityManager.class, EnableTransactionManagement.class, EntityManager.class })
HibernateEntityManager.class }) @Conditional(HibernateEntityManagerCondition.class)
@ConditionalOnBean(DataSource.class) @ConditionalOnBean(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration implements public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration implements
@ -97,4 +103,20 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration implemen
} }
return "create-drop"; return "create-drop";
} }
public static class HibernateEntityManagerCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (ClassUtils.isPresent("org.hibernate.ejb.HibernateEntityManager",
context.getClassLoader())
|| ClassUtils.isPresent("org.hibernate.jpa.HibernateEntityManager",
context.getClassLoader())) {
return ConditionOutcome.match("found HibernateEntityManager class");
}
return ConditionOutcome.noMatch("did not find HibernateEntityManager class");
}
}
} }

Loading…
Cancel
Save