|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.jpa;
|
18 | 18 |
|
| 19 | +import java.util.Map; |
| 20 | + |
19 | 21 | import jakarta.persistence.EntityManagerFactory;
|
| 22 | +import jakarta.persistence.metamodel.Metamodel; |
20 | 23 | import org.junit.jupiter.api.Test;
|
21 | 24 |
|
| 25 | +import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor; |
22 | 26 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
23 | 27 | import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
24 | 28 | import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
|
40 | 44 | import org.springframework.context.annotation.Import;
|
41 | 45 | import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
42 | 46 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
| 47 | +import org.springframework.data.jpa.util.JpaMetamodel; |
43 | 48 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
44 | 49 | import org.springframework.scheduling.annotation.EnableScheduling;
|
| 50 | +import org.springframework.test.util.ReflectionTestUtils; |
45 | 51 | import org.springframework.transaction.PlatformTransactionManager;
|
46 | 52 |
|
47 | 53 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -138,6 +144,19 @@ void bootstrapModeIsDefaultByDefault() {
|
138 | 144 | .isNull());
|
139 | 145 | }
|
140 | 146 |
|
| 147 | + @Test |
| 148 | + void whenLazyInitializationIsEnabledJpaMetamodelCacheIsClearedOnContextClose() { |
| 149 | + this.contextRunner.withUserConfiguration(TestConfiguration.class) |
| 150 | + .withBean(LazyInitializationBeanFactoryPostProcessor.class) |
| 151 | + .run((context) -> assertThat(jpaMetamodelCache()).isNotEmpty()); |
| 152 | + assertThat(jpaMetamodelCache()).isEmpty(); |
| 153 | + } |
| 154 | + |
| 155 | + @SuppressWarnings("unchecked") |
| 156 | + private Map<Metamodel, JpaMetamodel> jpaMetamodelCache() { |
| 157 | + return (Map<Metamodel, JpaMetamodel>) ReflectionTestUtils.getField(JpaMetamodel.class, "CACHE"); |
| 158 | + } |
| 159 | + |
141 | 160 | @Configuration(proxyBeanMethods = false)
|
142 | 161 | @EnableScheduling
|
143 | 162 | @Import(TestConfiguration.class)
|
|
0 commit comments