From c45604b8256b489124f517b34b9071752de2155c Mon Sep 17 00:00:00 2001 From: Manuel Doninger Date: Wed, 14 Jan 2015 17:36:52 +0100 Subject: [PATCH 1/2] Ensure that Hibernate's JtaPlatform can be customized Previously, when JpaProperties was applying additional properties it discarded any existing properties. This meant that custom configuration of Hibernate's JtaPlatform was not detected and that Boot's SpringJtaPlatform would always be configured instead. This commit updates JpaProperties to include all of the existing properties when it's asked to apply any additional properties. See gh-2348 Closes gh-2359 --- .../boot/autoconfigure/orm/jpa/JpaProperties.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java index 6f894e502d..72d479f497 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -168,7 +168,7 @@ public class JpaProperties { private Map getAdditionalProperties(Map existing, DataSource dataSource) { - Map result = new HashMap(); + Map result = new HashMap(existing); if (!isAlreadyProvided(existing, "ejb.naming_strategy") && this.namingStrategy != null) { result.put("hibernate.ejb.naming_strategy", this.namingStrategy.getName()); From 42e1a0b0690167f23a2dcc3a07cbb4faaed871dd Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 20 Jan 2015 08:58:45 +0000 Subject: [PATCH 2/2] Add a test to verify customization of Hibernate's JtaPlatform Closes gh-2348 --- .../HibernateJpaAutoConfigurationTests.java | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java index 328ce7e087..16a290c7f0 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -16,11 +16,21 @@ package org.springframework.boot.autoconfigure.orm.jpa; -import javax.sql.DataSource; +import java.util.Map; +import javax.sql.DataSource; +import javax.transaction.Synchronization; +import javax.transaction.SystemException; +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; +import javax.transaction.UserTransaction; + +import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; +import org.springframework.boot.autoconfigure.jta.JtaAutoConfiguration; +import org.springframework.boot.autoconfigure.jta.JtaProperties; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.jdbc.core.JdbcTemplate; @@ -138,4 +148,52 @@ public class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigura this.context.refresh(); } + @Test + public void testCustomJtaPlatform() throws Exception { + EnvironmentTestUtils.addEnvironment(this.context, + "spring.jpa.properties.hibernate.transaction.jta.platform:" + + TestJtaPlatform.class.getName()); + this.context.register(JtaProperties.class, JtaAutoConfiguration.class); + setupTestConfiguration(); + this.context.refresh(); + Map jpaPropertyMap = this.context.getBean( + LocalContainerEntityManagerFactoryBean.class).getJpaPropertyMap(); + assertThat((String) jpaPropertyMap.get("hibernate.transaction.jta.platform"), + equalTo(TestJtaPlatform.class.getName())); + } + + public static class TestJtaPlatform implements JtaPlatform { + + @Override + public TransactionManager retrieveTransactionManager() { + throw new UnsupportedOperationException(); + } + + @Override + public UserTransaction retrieveUserTransaction() { + throw new UnsupportedOperationException(); + } + + @Override + public Object getTransactionIdentifier(Transaction transaction) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean canRegisterSynchronization() { + throw new UnsupportedOperationException(); + } + + @Override + public void registerSynchronization(Synchronization synchronization) { + throw new UnsupportedOperationException(); + } + + @Override + public int getCurrentStatus() throws SystemException { + throw new UnsupportedOperationException(); + } + + } + }