Use reflection to load Hibernate version specific classes
This adds support for Hibernate 4.2 again by loading the specific classes with reflection. Fixes gh-1460, fixes gh-1557
This commit is contained in:
		
							parent
							
								
									9af8fdb8a1
								
							
						
					
					
						commit
						02e33c125c
					
				| 
						 | 
					@ -22,7 +22,8 @@ import java.util.Map;
 | 
				
			||||||
import javax.persistence.EntityManager;
 | 
					import javax.persistence.EntityManager;
 | 
				
			||||||
import javax.sql.DataSource;
 | 
					import javax.sql.DataSource;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 | 
					import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 | 
					import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 | 
				
			||||||
| 
						 | 
					@ -36,8 +37,6 @@ import org.springframework.boot.orm.jpa.hibernate.SpringJtaPlatform;
 | 
				
			||||||
import org.springframework.context.annotation.ConditionContext;
 | 
					import org.springframework.context.annotation.ConditionContext;
 | 
				
			||||||
import org.springframework.context.annotation.Conditional;
 | 
					import org.springframework.context.annotation.Conditional;
 | 
				
			||||||
import org.springframework.context.annotation.Configuration;
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
import org.springframework.core.Ordered;
 | 
					 | 
				
			||||||
import org.springframework.core.annotation.Order;
 | 
					 | 
				
			||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
 | 
					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;
 | 
				
			||||||
| 
						 | 
					@ -61,6 +60,9 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final String JTA_PLATFORM = "hibernate.transaction.jta.platform";
 | 
						private static final String JTA_PLATFORM = "hibernate.transaction.jta.platform";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static final Logger logger = LoggerFactory
 | 
				
			||||||
 | 
								.getLogger(HibernateJpaAutoConfiguration.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Autowired
 | 
						@Autowired
 | 
				
			||||||
	private JpaProperties properties;
 | 
						private JpaProperties properties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -82,19 +84,38 @@ public class HibernateJpaAutoConfiguration extends JpaBaseConfiguration {
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
 | 
						protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
 | 
				
			||||||
		super.customizeVendorProperties(vendorProperties);
 | 
							super.customizeVendorProperties(vendorProperties);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							String HIBERNATE43_NOJTAPLATFORM_CLASS = "org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform";
 | 
				
			||||||
 | 
							String HIBERNATE42_NOJTAPLATFORM_CLASS = "org.hibernate.service.jta.platform.internal.NoJtaPlatform";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (!vendorProperties.containsKey(JTA_PLATFORM)) {
 | 
							if (!vendorProperties.containsKey(JTA_PLATFORM)) {
 | 
				
			||||||
			JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
 | 
								JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
 | 
				
			||||||
			if (jtaTransactionManager != null) {
 | 
								try {
 | 
				
			||||||
				vendorProperties.put(JTA_PLATFORM, new SpringJtaPlatform(
 | 
									if (jtaTransactionManager != null) {
 | 
				
			||||||
						jtaTransactionManager));
 | 
										vendorProperties.put(JTA_PLATFORM, new SpringJtaPlatform(
 | 
				
			||||||
 | 
												jtaTransactionManager));
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									else {
 | 
				
			||||||
 | 
										Object jtaPlatform = null;
 | 
				
			||||||
 | 
										if (ClassUtils.isPresent(HIBERNATE43_NOJTAPLATFORM_CLASS, null)) {
 | 
				
			||||||
 | 
											jtaPlatform = ClassUtils.forName(HIBERNATE43_NOJTAPLATFORM_CLASS,
 | 
				
			||||||
 | 
													null).newInstance();
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										else if (ClassUtils.isPresent(HIBERNATE42_NOJTAPLATFORM_CLASS, null)) {
 | 
				
			||||||
 | 
											jtaPlatform = ClassUtils.forName(HIBERNATE42_NOJTAPLATFORM_CLASS,
 | 
				
			||||||
 | 
													null).newInstance();
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										if (jtaPlatform != null) {
 | 
				
			||||||
 | 
											vendorProperties.put(JTA_PLATFORM, jtaPlatform);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			else {
 | 
								catch (Exception e) {
 | 
				
			||||||
				vendorProperties.put(JTA_PLATFORM, NoJtaPlatform.INSTANCE);
 | 
									logger.error("Could not configure the JTA platform", e);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Order(Ordered.HIGHEST_PRECEDENCE + 20)
 | 
					 | 
				
			||||||
	static class HibernateEntityManagerCondition extends SpringBootCondition {
 | 
						static class HibernateEntityManagerCondition extends SpringBootCondition {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private static String[] CLASS_NAMES = {
 | 
							private static String[] CLASS_NAMES = {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -66,7 +66,7 @@
 | 
				
			||||||
		<module>spring-boot-sample-websocket</module>
 | 
							<module>spring-boot-sample-websocket</module>
 | 
				
			||||||
		<module>spring-boot-sample-ws</module>
 | 
							<module>spring-boot-sample-ws</module>
 | 
				
			||||||
		<module>spring-boot-sample-xml</module>
 | 
							<module>spring-boot-sample-xml</module>
 | 
				
			||||||
	</modules>
 | 
					    </modules>
 | 
				
			||||||
	<!-- No dependencies - otherwise the samples won't work if you change the
 | 
						<!-- No dependencies - otherwise the samples won't work if you change the
 | 
				
			||||||
		parent -->
 | 
							parent -->
 | 
				
			||||||
	<build>
 | 
						<build>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue