Allow to configure PersistenceUnitPostProcessor
This commit updates EntityManagerFactoryBuilder so that persistence unit post processors can be registered and applied when creating an EntityManagerFactory. See gh-25443
This commit is contained in:
committed by
Stephane Nicoll
parent
67f10e585a
commit
c3c83c8a6c
@@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.spi.PersistenceUnitInfo;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
||||
@@ -229,6 +230,19 @@ abstract class AbstractJpaAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customPersistenceUnitPostProcessors() {
|
||||
this.contextRunner.withUserConfiguration(TestConfigurationWithCustomPersistenceUnitPostProcessors.class)
|
||||
.run((context) -> {
|
||||
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = context
|
||||
.getBean(LocalContainerEntityManagerFactoryBean.class);
|
||||
PersistenceUnitInfo persistenceUnitInfo = entityManagerFactoryBean.getPersistenceUnitInfo();
|
||||
assertThat(persistenceUnitInfo).isNotNull();
|
||||
assertThat(persistenceUnitInfo.getManagedClassNames())
|
||||
.contains("customized.attribute.converter.class.name");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class TestTwoDataSourcesConfiguration {
|
||||
|
||||
@@ -388,6 +402,18 @@ abstract class AbstractJpaAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@TestAutoConfigurationPackage(AbstractJpaAutoConfigurationTests.class)
|
||||
static class TestConfigurationWithCustomPersistenceUnitPostProcessors {
|
||||
|
||||
@Bean
|
||||
EntityManagerFactoryBuilderCustomizer entityManagerFactoryBuilderCustomizer() {
|
||||
return (builder) -> builder.setPersistenceUnitPostProcessors(
|
||||
(pui) -> pui.addManagedClassName("customized.attribute.converter.class.name"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
static class CustomJpaTransactionManager extends JpaTransactionManager {
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
|
||||
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -58,6 +59,8 @@ public class EntityManagerFactoryBuilder {
|
||||
|
||||
private AsyncTaskExecutor bootstrapExecutor;
|
||||
|
||||
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
|
||||
|
||||
/**
|
||||
* Create a new instance passing in the common pieces that will be shared if multiple
|
||||
* EntityManagerFactory instances are created.
|
||||
@@ -104,6 +107,16 @@ public class EntityManagerFactoryBuilder {
|
||||
this.bootstrapExecutor = bootstrapExecutor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the PersistenceUnitPostProcessors to be applied to the PersistenceUnitInfo used
|
||||
* for creating this EntityManagerFactory.
|
||||
* @param persistenceUnitPostProcessors internal persistence unit post processors
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... persistenceUnitPostProcessors) {
|
||||
this.persistenceUnitPostProcessors = persistenceUnitPostProcessors;
|
||||
}
|
||||
|
||||
/**
|
||||
* A fluent builder for a LocalContainerEntityManagerFactoryBean.
|
||||
*/
|
||||
@@ -232,6 +245,8 @@ public class EntityManagerFactoryBuilder {
|
||||
if (EntityManagerFactoryBuilder.this.bootstrapExecutor != null) {
|
||||
entityManagerFactoryBean.setBootstrapExecutor(EntityManagerFactoryBuilder.this.bootstrapExecutor);
|
||||
}
|
||||
entityManagerFactoryBean
|
||||
.setPersistenceUnitPostProcessors(EntityManagerFactoryBuilder.this.persistenceUnitPostProcessors);
|
||||
return entityManagerFactoryBean;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user