Skip to content

Commit 6e62bc9

Browse files
committed
Refactor PersistenceUnitContext to AotEntityManagerFactoryCreator.
Simplify arrangement as we really only require EntityManagerFactory and can derive related objects from there.
1 parent 2add393 commit 6e62bc9

File tree

4 files changed

+161
-223
lines changed

4 files changed

+161
-223
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.aot;
17+
18+
import jakarta.persistence.Converter;
19+
import jakarta.persistence.Embeddable;
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.EntityManagerFactory;
22+
import jakarta.persistence.MappedSuperclass;
23+
import jakarta.persistence.spi.PersistenceUnitInfo;
24+
25+
import java.util.List;
26+
import java.util.function.Supplier;
27+
28+
import org.springframework.data.repository.config.AotRepositoryContext;
29+
import org.springframework.data.util.Lazy;
30+
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
31+
import org.springframework.util.ObjectUtils;
32+
33+
/**
34+
* Wrapper for {@link EntityManagerFactory}. The wrapper object implements equality checks based on its creation
35+
* arguments and can be conveniently used as cache key.
36+
* <p>
37+
* Factory methods provide ways to create instances based on provided {@link EntityManagerFactory} or contextual holders
38+
* to extract managed types and create an in-memory {@link EntityManagerFactory} variant for metamodel introspection
39+
* during AOT processing.
40+
*
41+
* @author Mark Paluch
42+
* @since 4.0
43+
*/
44+
public class AotEntityManagerFactoryCreator {
45+
46+
private final Supplier<EntityManagerFactory> factory;
47+
private final Object key;
48+
49+
private AotEntityManagerFactoryCreator(Supplier<EntityManagerFactory> factory, Object key) {
50+
this.factory = Lazy.of(factory);
51+
this.key = key;
52+
}
53+
54+
/**
55+
* Create a {@code PersistenceUnitContext} from the given {@link AotRepositoryContext} using Jakarta
56+
* Persistence-annotated classes.
57+
* <p>
58+
* The underlying {@link jakarta.persistence.metamodel.Metamodel} requires Hibernate to build metamodel information.
59+
*
60+
* @param repositoryContext repository context providing classes.
61+
*/
62+
public static AotEntityManagerFactoryCreator from(AotRepositoryContext repositoryContext) {
63+
64+
List<String> typeNames = repositoryContext.getResolvedTypes().stream()
65+
.filter(AotEntityManagerFactoryCreator::isJakartaAnnotated).map(Class::getName).toList();
66+
67+
return from(PersistenceManagedTypes.of(typeNames, List.of()), typeNames);
68+
}
69+
70+
/**
71+
* Create a {@code PersistenceUnitContext} from the given {@link PersistenceUnitInfo}.
72+
* <p>
73+
* The underlying {@link jakarta.persistence.metamodel.Metamodel} requires Hibernate to build metamodel information.
74+
*
75+
* @param persistenceUnitInfo persistence unit info to use.
76+
*/
77+
public static AotEntityManagerFactoryCreator from(PersistenceUnitInfo persistenceUnitInfo) {
78+
return from(() -> new AotMetamodel(persistenceUnitInfo), persistenceUnitInfo);
79+
}
80+
81+
/**
82+
* Create a {@code PersistenceUnitContext} from the given {@link PersistenceManagedTypes}.
83+
* <p>
84+
* The underlying {@link jakarta.persistence.metamodel.Metamodel} requires Hibernate to build metamodel information.
85+
*
86+
* @param managedTypes managed types to use.
87+
*/
88+
public static AotEntityManagerFactoryCreator from(PersistenceManagedTypes managedTypes) {
89+
return from(managedTypes, managedTypes);
90+
}
91+
92+
private static AotEntityManagerFactoryCreator from(PersistenceManagedTypes managedTypes, Object cacheKey) {
93+
return from(() -> new AotMetamodel(managedTypes), cacheKey);
94+
}
95+
96+
/**
97+
* Create a {@code PersistenceUnitContext} from the given {@link EntityManagerFactory}.
98+
*
99+
* @param entityManagerFactory the entity manager factory to use.
100+
*/
101+
public static AotEntityManagerFactoryCreator just(EntityManagerFactory entityManagerFactory) {
102+
return new AotEntityManagerFactoryCreator(() -> entityManagerFactory, entityManagerFactory.getMetamodel());
103+
}
104+
105+
private static AotEntityManagerFactoryCreator from(Supplier<? extends AotMetamodel> metamodel, Object key) {
106+
return new AotEntityManagerFactoryCreator(() -> metamodel.get().getEntityManagerFactory(), key);
107+
}
108+
109+
private static boolean isJakartaAnnotated(Class<?> cls) {
110+
111+
return cls.isAnnotationPresent(Entity.class) //
112+
|| cls.isAnnotationPresent(Embeddable.class) //
113+
|| cls.isAnnotationPresent(MappedSuperclass.class) //
114+
|| cls.isAnnotationPresent(Converter.class);
115+
}
116+
117+
/**
118+
* Return the {@link EntityManagerFactory}.
119+
*
120+
* @return the entity manager factory to use during AOT processing.
121+
*/
122+
public EntityManagerFactory getEntityManagerFactory() {
123+
return factory.get();
124+
}
125+
126+
@Override
127+
public boolean equals(Object o) {
128+
if (!(o instanceof AotEntityManagerFactoryCreator that)) {
129+
return false;
130+
}
131+
return ObjectUtils.nullSafeEquals(key, that.key);
132+
}
133+
134+
@Override
135+
public int hashCode() {
136+
return ObjectUtils.nullSafeHashCode(key);
137+
}
138+
139+
@Override
140+
public String toString() {
141+
return "AotEntityManagerFactory{" + key + '}';
142+
}
143+
144+
}

0 commit comments

Comments
 (0)