Merge pull request #25443 from JohnNiang

* pr/25443:
  Polish "Allow to configure PersistenceUnitPostProcessor"
  Allow to configure PersistenceUnitPostProcessor

Closes gh-25443
This commit is contained in:
Stephane Nicoll
2021-02-27 11:38:36 +01:00
2 changed files with 46 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@@ -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 {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
@@ -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,17 @@ public class EntityManagerFactoryBuilder {
this.bootstrapExecutor = bootstrapExecutor;
}
/**
* Set the {@linkplain PersistenceUnitPostProcessor persistence unit post processors}
* to be applied to the PersistenceUnitInfo used for creating the
* {@link LocalContainerEntityManagerFactoryBean}.
* @param persistenceUnitPostProcessors the persistence unit post processors to use
* @since 2.5.0
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... persistenceUnitPostProcessors) {
this.persistenceUnitPostProcessors = persistenceUnitPostProcessors;
}
/**
* A fluent builder for a LocalContainerEntityManagerFactoryBean.
*/
@@ -232,6 +246,10 @@ public class EntityManagerFactoryBuilder {
if (EntityManagerFactoryBuilder.this.bootstrapExecutor != null) {
entityManagerFactoryBean.setBootstrapExecutor(EntityManagerFactoryBuilder.this.bootstrapExecutor);
}
if (EntityManagerFactoryBuilder.this.persistenceUnitPostProcessors != null) {
entityManagerFactoryBean.setPersistenceUnitPostProcessors(
EntityManagerFactoryBuilder.this.persistenceUnitPostProcessors);
}
return entityManagerFactoryBean;
}