|
16 | 16 |
|
17 | 17 | package org.springframework.boot.data.jpa.autoconfigure;
|
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;
|
|
35 | 39 | import org.springframework.context.annotation.Import;
|
36 | 40 | import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
37 | 41 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
| 42 | +import org.springframework.data.jpa.util.JpaMetamodel; |
38 | 43 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
39 | 44 | import org.springframework.scheduling.annotation.EnableScheduling;
|
| 45 | +import org.springframework.test.util.ReflectionTestUtils; |
40 | 46 | import org.springframework.transaction.PlatformTransactionManager;
|
41 | 47 |
|
42 | 48 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -133,6 +139,19 @@ void bootstrapModeIsDefaultByDefault() {
|
133 | 139 | .isNull());
|
134 | 140 | }
|
135 | 141 |
|
| 142 | + @Test |
| 143 | + void whenLazyInitializationIsEnabledJpaMetamodelCacheIsClearedOnContextClose() { |
| 144 | + this.contextRunner.withUserConfiguration(TestConfiguration.class) |
| 145 | + .withBean(LazyInitializationBeanFactoryPostProcessor.class) |
| 146 | + .run((context) -> assertThat(jpaMetamodelCache()).isNotEmpty()); |
| 147 | + assertThat(jpaMetamodelCache()).isEmpty(); |
| 148 | + } |
| 149 | + |
| 150 | + @SuppressWarnings("unchecked") |
| 151 | + private Map<Metamodel, JpaMetamodel> jpaMetamodelCache() { |
| 152 | + return (Map<Metamodel, JpaMetamodel>) ReflectionTestUtils.getField(JpaMetamodel.class, "CACHE"); |
| 153 | + } |
| 154 | + |
136 | 155 | @Configuration(proxyBeanMethods = false)
|
137 | 156 | @EnableScheduling
|
138 | 157 | @Import(TestConfiguration.class)
|
|
0 commit comments