Skip to content

Commit f3cf517

Browse files
committed
Polishing.
Run AotMetamodel against live EntityManagerFactory, use Environment to check for AOT repository enabled flag. See #3830
1 parent f9cdca1 commit f3cf517

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/AotMetamodel.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public EntityManager entityManager() {
9696
return entityManager.get();
9797
}
9898

99-
// TODO: Capture an existing factory bean (e.g. EntityManagerFactoryInfo) to extract PersistenceInfo
10099
public EntityManagerFactory getEntityManagerFactory() {
101100
return entityManagerFactory.get();
102101
}
@@ -125,7 +124,8 @@ public void addTransformer(ClassTransformer classTransformer) {
125124
public List<String> getManagedClassNames() {
126125
return persistenceUnitInfo.getManagedClassNames();
127126
}
128-
}, Map.of("hibernate.dialect", "org.hibernate.dialect.H2Dialect")).build();
127+
}, Map.of("hibernate.dialect", "org.hibernate.dialect.H2Dialect", "hibernate.boot.allow_jdbc_metadata_access",
128+
"false")).build();
129129
}
130130

131131
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/aot/JpaRepositoryContributor.java

-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ protected void customizeConstructor(AotRepositoryConstructorBuilder constructorB
189189
MergedAnnotation<EntityGraph> entityGraph = context.getAnnotation(EntityGraph.class);
190190
MergedAnnotation<Modifying> modifying = context.getAnnotation(Modifying.class);
191191

192-
body.add(context.codeBlocks().logDebug("invoking [%s]".formatted(context.getMethod().getName())));
193-
194192
AotEntityGraph aotEntityGraph = entityGraphLookup.findEntityGraph(entityGraph, repositoryInformation,
195193
returnedType, queryMethod);
196194

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/config/JpaRepositoryConfigExtension.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.springframework.data.jpa.repository.config.BeanDefinitionNames.*;
1919

2020
import jakarta.persistence.Entity;
21+
import jakarta.persistence.EntityManagerFactory;
2122
import jakarta.persistence.MappedSuperclass;
2223
import jakarta.persistence.PersistenceContext;
2324
import jakarta.persistence.PersistenceUnit;
@@ -38,6 +39,7 @@
3839
import org.springframework.aot.generate.GenerationContext;
3940
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
4041
import org.springframework.beans.factory.config.BeanDefinition;
42+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4143
import org.springframework.beans.factory.support.AbstractBeanDefinition;
4244
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
4345
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -325,15 +327,20 @@ static boolean isActive(@Nullable ClassLoader classLoader) {
325327
*/
326328
public static class JpaRepositoryRegistrationAotProcessor extends RepositoryRegistrationAotProcessor {
327329

328-
protected RepositoryContributor contribute(AotRepositoryContext repositoryContext, GenerationContext generationContext) {
330+
protected @Nullable RepositoryContributor contribute(AotRepositoryContext repositoryContext,
331+
GenerationContext generationContext) {
329332

330-
// don't register domain types nor annotations.
331-
332-
if (!AotContext.aotGeneratedRepositoriesEnabled()) {
333+
boolean enabled = Boolean.parseBoolean(
334+
repositoryContext.getEnvironment().getProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "false"));
335+
if (!enabled) {
333336
return null;
334337
}
335338

336-
return new JpaRepositoryContributor(repositoryContext);
339+
ConfigurableListableBeanFactory beanFactory = repositoryContext.getBeanFactory();
340+
EntityManagerFactory emf = beanFactory.getBeanProvider(EntityManagerFactory.class).getIfAvailable();
341+
342+
return emf != null ? new JpaRepositoryContributor(repositoryContext, emf)
343+
: new JpaRepositoryContributor(repositoryContext);
337344
}
338345

339346
@Nullable

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/aot/TestJpaAotRepositoryContext.java

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2727
import org.springframework.core.annotation.MergedAnnotation;
28+
import org.springframework.core.env.Environment;
29+
import org.springframework.core.env.StandardEnvironment;
2830
import org.springframework.core.io.ClassPathResource;
2931
import org.springframework.core.test.tools.ClassFile;
3032
import org.springframework.data.jpa.domain.sample.Role;
@@ -35,6 +37,8 @@
3537
import org.springframework.lang.Nullable;
3638

3739
/**
40+
* Test {@link AotRepositoryContext} implementation for JPA repositories.
41+
*
3842
* @author Christoph Strobl
3943
*/
4044
public class TestJpaAotRepositoryContext<T> implements AotRepositoryContext {
@@ -56,6 +60,11 @@ public ConfigurableListableBeanFactory getBeanFactory() {
5660
return null;
5761
}
5862

63+
@Override
64+
public Environment getEnvironment() {
65+
return new StandardEnvironment();
66+
}
67+
5968
@Override
6069
public TypeIntrospector introspectType(String typeName) {
6170
return null;

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/config/JpaRepositoryRegistrationAotProcessorUnitTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
3232
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3333
import org.springframework.core.annotation.MergedAnnotation;
34+
import org.springframework.core.env.Environment;
35+
import org.springframework.core.env.StandardEnvironment;
3436
import org.springframework.data.repository.config.AotRepositoryContext;
3537
import org.springframework.data.repository.core.RepositoryInformation;
3638
import org.springframework.javapoet.ClassName;
@@ -116,6 +118,11 @@ public ConfigurableListableBeanFactory getBeanFactory() {
116118
return null;
117119
}
118120

121+
@Override
122+
public Environment getEnvironment() {
123+
return new StandardEnvironment();
124+
}
125+
119126
@Override
120127
public TypeIntrospector introspectType(String typeName) {
121128
return null;

0 commit comments

Comments
 (0)