commit
c48228f645
|
@ -58,11 +58,17 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
|
||||||
|
|
||||||
private HibernateProperties hibernateProperties;
|
private HibernateProperties hibernateProperties;
|
||||||
|
|
||||||
|
private DataSourceSchemaCreatedPublisher schemaCreatedPublisher;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||||
if (bean instanceof LocalContainerEntityManagerFactoryBean) {
|
if (bean instanceof LocalContainerEntityManagerFactoryBean) {
|
||||||
LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean) bean;
|
LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean) bean;
|
||||||
factory.setJpaVendorAdapter(new DataSourceSchemaCreatedPublisher(factory));
|
if (factory.getBootstrapExecutor() != null && factory.getJpaVendorAdapter() != null) {
|
||||||
|
this.schemaCreatedPublisher = new DataSourceSchemaCreatedPublisher(factory.getBootstrapExecutor(),
|
||||||
|
factory.getJpaVendorAdapter());
|
||||||
|
factory.setJpaVendorAdapter(this.schemaCreatedPublisher);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
@ -79,12 +85,10 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
|
||||||
if (bean instanceof HibernateProperties) {
|
if (bean instanceof HibernateProperties) {
|
||||||
this.hibernateProperties = (HibernateProperties) bean;
|
this.hibernateProperties = (HibernateProperties) bean;
|
||||||
}
|
}
|
||||||
if (bean instanceof LocalContainerEntityManagerFactoryBean) {
|
if (bean instanceof LocalContainerEntityManagerFactoryBean && this.schemaCreatedPublisher == null) {
|
||||||
LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean) bean;
|
LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean) bean;
|
||||||
if (factory.getBootstrapExecutor() == null) {
|
|
||||||
publishEventIfRequired(factory.getNativeEntityManagerFactory());
|
publishEventIfRequired(factory.getNativeEntityManagerFactory());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,13 +145,14 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
|
||||||
|
|
||||||
final class DataSourceSchemaCreatedPublisher implements JpaVendorAdapter {
|
final class DataSourceSchemaCreatedPublisher implements JpaVendorAdapter {
|
||||||
|
|
||||||
|
private final AsyncTaskExecutor bootstrapExecutor;
|
||||||
|
|
||||||
private final JpaVendorAdapter delegate;
|
private final JpaVendorAdapter delegate;
|
||||||
|
|
||||||
private final LocalContainerEntityManagerFactoryBean factory;
|
private DataSourceSchemaCreatedPublisher(AsyncTaskExecutor bootstrapExecutor,
|
||||||
|
JpaVendorAdapter jpaVendorAdapter) {
|
||||||
private DataSourceSchemaCreatedPublisher(LocalContainerEntityManagerFactoryBean factory) {
|
this.bootstrapExecutor = bootstrapExecutor;
|
||||||
this.delegate = factory.getJpaVendorAdapter();
|
this.delegate = jpaVendorAdapter;
|
||||||
this.factory = factory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -188,9 +193,8 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
|
||||||
@Override
|
@Override
|
||||||
public void postProcessEntityManagerFactory(EntityManagerFactory emf) {
|
public void postProcessEntityManagerFactory(EntityManagerFactory emf) {
|
||||||
this.delegate.postProcessEntityManagerFactory(emf);
|
this.delegate.postProcessEntityManagerFactory(emf);
|
||||||
AsyncTaskExecutor bootstrapExecutor = this.factory.getBootstrapExecutor();
|
if (this.bootstrapExecutor != null) {
|
||||||
if (bootstrapExecutor != null) {
|
this.bootstrapExecutor.execute(() -> DataSourceInitializedPublisher.this.publishEventIfRequired(emf));
|
||||||
bootstrapExecutor.execute(() -> DataSourceInitializedPublisher.this.publishEventIfRequired(emf));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,12 +23,14 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.sql.DataSource;
|
||||||
import javax.transaction.Synchronization;
|
import javax.transaction.Synchronization;
|
||||||
import javax.transaction.Transaction;
|
import javax.transaction.Transaction;
|
||||||
import javax.transaction.TransactionManager;
|
import javax.transaction.TransactionManager;
|
||||||
|
@ -42,6 +44,7 @@ import org.hibernate.dialect.H2Dialect;
|
||||||
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
|
||||||
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
|
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
|
||||||
import org.hibernate.internal.SessionFactoryImpl;
|
import org.hibernate.internal.SessionFactoryImpl;
|
||||||
|
import org.hibernate.jpa.HibernatePersistenceProvider;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
|
@ -393,6 +396,18 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenLocalContanerEntityManagerFactoryBeanHasNoJpaVendorAdapterAutoConfigurationSucceeds() {
|
||||||
|
contextRunner()
|
||||||
|
.withUserConfiguration(
|
||||||
|
TestConfigurationWithLocalContainerEntityManagerFactoryBeanWithNoJpaVendorAdapter.class)
|
||||||
|
.run((context) -> {
|
||||||
|
EntityManagerFactory factoryBean = context.getBean(EntityManagerFactory.class);
|
||||||
|
Map<String, Object> map = factoryBean.getProperties();
|
||||||
|
assertThat(map.get("configured")).isEqualTo("manually");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private boolean dataSourceSchemaCreatedEventReceived(EventCapturingApplicationListener listener) {
|
private boolean dataSourceSchemaCreatedEventReceived(EventCapturingApplicationListener listener) {
|
||||||
for (ApplicationEvent event : listener.events) {
|
for (ApplicationEvent event : listener.events) {
|
||||||
if (event instanceof DataSourceSchemaCreatedEvent) {
|
if (event instanceof DataSourceSchemaCreatedEvent) {
|
||||||
|
@ -556,6 +571,25 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class TestConfigurationWithLocalContainerEntityManagerFactoryBeanWithNoJpaVendorAdapter
|
||||||
|
extends TestConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
|
||||||
|
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
|
||||||
|
factoryBean.setDataSource(dataSource);
|
||||||
|
factoryBean.setPersistenceUnitName("manually-configured");
|
||||||
|
factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
|
||||||
|
Map<String, Object> properties = new HashMap<>();
|
||||||
|
properties.put("configured", "manually");
|
||||||
|
properties.put("hibernate.transaction.jta.platform", NoJtaPlatform.INSTANCE);
|
||||||
|
factoryBean.setJpaPropertyMap(properties);
|
||||||
|
return factoryBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static class TestH2Dialect extends H2Dialect {
|
public static class TestH2Dialect extends H2Dialect {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue