Skip to content

Commit a8c4240

Browse files
Polishing?
1 parent 2add393 commit a8c4240

File tree

5 files changed

+312
-213
lines changed

5 files changed

+312
-213
lines changed

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

Lines changed: 2 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,15 @@
1515
*/
1616
package org.springframework.data.jpa.repository.aot;
1717

18-
import jakarta.persistence.Converter;
19-
import jakarta.persistence.Embeddable;
20-
import jakarta.persistence.Entity;
2118
import jakarta.persistence.EntityManager;
2219
import jakarta.persistence.EntityManagerFactory;
23-
import jakarta.persistence.MappedSuperclass;
2420
import jakarta.persistence.PersistenceUnitUtil;
2521
import jakarta.persistence.metamodel.Metamodel;
26-
import jakarta.persistence.spi.PersistenceUnitInfo;
2722

2823
import java.lang.reflect.Method;
29-
import java.util.List;
3024
import java.util.Map;
3125
import java.util.Optional;
3226
import java.util.function.Function;
33-
import java.util.function.Supplier;
3427

3528
import org.jspecify.annotations.Nullable;
3629

@@ -65,12 +58,9 @@
6558
import org.springframework.data.repository.query.ParametersSource;
6659
import org.springframework.data.repository.query.QueryMethod;
6760
import org.springframework.data.repository.query.ReturnedType;
68-
import org.springframework.data.util.Lazy;
6961
import org.springframework.javapoet.CodeBlock;
7062
import org.springframework.javapoet.TypeName;
71-
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
7263
import org.springframework.util.ClassUtils;
73-
import org.springframework.util.ObjectUtils;
7464
import org.springframework.util.StringUtils;
7565

7666
/**
@@ -94,11 +84,11 @@ public class JpaRepositoryContributor extends RepositoryContributor {
9484
private final EntityGraphLookup entityGraphLookup;
9585

9686
public JpaRepositoryContributor(AotRepositoryContext repositoryContext) {
97-
this(repositoryContext, PersistenceUnitContextFactory.from(repositoryContext).create());
87+
this(repositoryContext, PersistenceUnitContexts.from(repositoryContext));
9888
}
9989

10090
public JpaRepositoryContributor(AotRepositoryContext repositoryContext, EntityManagerFactory entityManagerFactory) {
101-
this(repositoryContext, PersistenceUnitContext.just(entityManagerFactory));
91+
this(repositoryContext, PersistenceUnitContexts.from(entityManagerFactory));
10292
}
10393

10494
public JpaRepositoryContributor(AotRepositoryContext repositoryContext, PersistenceUnitContext persistenceUnit) {
@@ -262,193 +252,6 @@ public PersistenceUnitContext getPersistenceUnit() {
262252
return persistenceUnit;
263253
}
264254

265-
/**
266-
* Factory for deferred {@link PersistenceUnitContext} creation. Factory objects implement equality checks based on
267-
* their creation and can be used conveniently as cache keys.
268-
*/
269-
public static class PersistenceUnitContextFactory {
270-
271-
private final Supplier<? extends PersistenceUnitContext> factory;
272-
private final Object key;
273-
274-
private PersistenceUnitContextFactory(Supplier<? extends PersistenceUnitContext> factory, Object key) {
275-
this.factory = Lazy.of(factory);
276-
this.key = key;
277-
}
278-
279-
/**
280-
* Create a {@code PersistenceUnitContext} from the given {@link AotRepositoryContext} using Jakarta
281-
* Persistence-annotated classes.
282-
*
283-
* @param repositoryContext repository context providing classes.
284-
*/
285-
public static PersistenceUnitContextFactory from(AotRepositoryContext repositoryContext) {
286-
287-
List<String> typeNames = repositoryContext.getResolvedTypes().stream()
288-
.filter(PersistenceUnitContextFactory::isJakartaAnnotated).map(Class::getName).toList();
289-
290-
return from(() -> new AotMetamodel(PersistenceManagedTypes.of(typeNames, List.of())), typeNames);
291-
}
292-
293-
/**
294-
* Create a {@code PersistenceUnitContext} from the given {@link PersistenceUnitInfo}.
295-
*
296-
* @param persistenceUnitInfo persistence unit info to use.
297-
*/
298-
public static PersistenceUnitContextFactory from(PersistenceUnitInfo persistenceUnitInfo) {
299-
return from(() -> new AotMetamodel(persistenceUnitInfo), persistenceUnitInfo);
300-
}
301-
302-
/**
303-
* Create a {@code PersistenceUnitContext} from the given {@link PersistenceManagedTypes}.
304-
*
305-
* @param managedTypes managed types to use.
306-
*/
307-
public static PersistenceUnitContextFactory from(PersistenceManagedTypes managedTypes) {
308-
return from(() -> new AotMetamodel(managedTypes), managedTypes);
309-
}
310-
311-
/**
312-
* Create a {@code PersistenceUnitContext} from the given {@link EntityManagerFactory} and its {@link Metamodel}.
313-
*
314-
* @param entityManagerFactory the entity manager factory to use.
315-
*/
316-
public static PersistenceUnitContextFactory just(EntityManagerFactory entityManagerFactory) {
317-
return new PersistenceUnitContextFactory(() -> new EntityManagerPersistenceUnitContext(entityManagerFactory),
318-
entityManagerFactory.getMetamodel());
319-
}
320-
321-
/**
322-
* Create a {@code PersistenceUnitContext} from the given {@link EntityManagerFactory} and its {@link Metamodel}.
323-
*
324-
* @param metamodel the metamodel to use.
325-
* @param entityManagerFactory the entity manager factory to use.
326-
*/
327-
public static PersistenceUnitContextFactory just(EntityManagerFactory entityManagerFactory, Metamodel metamodel) {
328-
return new PersistenceUnitContextFactory(
329-
() -> new EntityManagerPersistenceUnitContext(entityManagerFactory, metamodel), metamodel);
330-
}
331-
332-
private static PersistenceUnitContextFactory from(Supplier<? extends AotMetamodel> metamodel, Object key) {
333-
return new PersistenceUnitContextFactory(() -> new AotMetamodelContext(metamodel.get()), key);
334-
}
335-
336-
private static boolean isJakartaAnnotated(Class<?> cls) {
337-
338-
return cls.isAnnotationPresent(Entity.class) //
339-
|| cls.isAnnotationPresent(Embeddable.class) //
340-
|| cls.isAnnotationPresent(MappedSuperclass.class) //
341-
|| cls.isAnnotationPresent(Converter.class);
342-
}
343-
344-
public PersistenceUnitContext create() {
345-
return factory.get();
346-
}
347-
348-
@Override
349-
public boolean equals(Object o) {
350-
if (!(o instanceof PersistenceUnitContextFactory that)) {
351-
return false;
352-
}
353-
return ObjectUtils.nullSafeEquals(key, that.key);
354-
}
355-
356-
@Override
357-
public int hashCode() {
358-
return ObjectUtils.nullSafeHashCode(key);
359-
}
360-
361-
@Override
362-
public String toString() {
363-
return "PersistenceUnitContextFactory{" + key + '}';
364-
}
365-
366-
}
367-
368-
/**
369-
* Strategy interface representing a JPA PersistenceUnit providing access to {@link EntityManagerFactory} and
370-
* {@link Metamodel} for AOT repository generation.
371-
*/
372-
public interface PersistenceUnitContext {
373-
374-
/**
375-
* @return the entity manager factory.
376-
*/
377-
EntityManagerFactory getEntityManagerFactory();
378-
379-
/**
380-
* @return metamodel describing managed types used in the persistence unit.
381-
*/
382-
Metamodel getMetamodel();
383-
384-
/**
385-
* Create a {@code PersistenceUnitContext} from the given {@link PersistenceUnitInfo}.
386-
*
387-
* @param persistenceUnitInfo persistence unit info to use.
388-
*/
389-
static PersistenceUnitContext from(PersistenceUnitInfo persistenceUnitInfo) {
390-
return new AotMetamodelContext(new AotMetamodel(persistenceUnitInfo));
391-
}
392-
393-
/**
394-
* Create a {@code PersistenceUnitContext} from the given {@link PersistenceManagedTypes}.
395-
*
396-
* @param managedTypes managed types to use.
397-
*/
398-
static PersistenceUnitContext from(PersistenceManagedTypes managedTypes) {
399-
return new AotMetamodelContext(new AotMetamodel(managedTypes));
400-
}
401-
402-
/**
403-
* Create a {@code PersistenceUnitContext} from the given {@link EntityManagerFactory} and its {@link Metamodel}.
404-
*
405-
* @param entityManagerFactory the entity manager factory to use.
406-
*/
407-
static PersistenceUnitContext just(EntityManagerFactory entityManagerFactory) {
408-
return new EntityManagerPersistenceUnitContext(entityManagerFactory);
409-
}
410-
411-
}
412-
413-
/**
414-
* Persistence unit context backed by an {@link EntityManagerFactory}.
415-
*/
416-
record EntityManagerPersistenceUnitContext(EntityManagerFactory factory,
417-
Metamodel metamodel) implements PersistenceUnitContext {
418-
419-
public EntityManagerPersistenceUnitContext(EntityManagerFactory factory) {
420-
this(factory, factory.getMetamodel());
421-
}
422-
423-
@Override
424-
public Metamodel getMetamodel() {
425-
return metamodel();
426-
}
427-
428-
@Override
429-
public EntityManagerFactory getEntityManagerFactory() {
430-
return factory();
431-
}
432-
433-
}
434-
435-
/**
436-
* Persistence unit context backed by an {@link AotMetamodel}.
437-
*/
438-
private record AotMetamodelContext(AotMetamodel metamodel) implements PersistenceUnitContext {
439-
440-
@Override
441-
public EntityManagerFactory getEntityManagerFactory() {
442-
return metamodel.getEntityManagerFactory();
443-
}
444-
445-
@Override
446-
public Metamodel getMetamodel() {
447-
return metamodel;
448-
}
449-
450-
}
451-
452255
record StoredProcedureMetadata(String procedure) implements QueryMetadata {
453256

454257
@Override
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
* http://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.EntityManagerFactory;
19+
import jakarta.persistence.metamodel.Metamodel;
20+
21+
/**
22+
* Strategy interface representing a JPA PersistenceUnit providing access to {@link EntityManagerFactory} and
23+
* {@link Metamodel} for AOT repository generation.
24+
*/
25+
public interface PersistenceUnitContext {
26+
27+
/**
28+
* @return the entity manager factory.
29+
*/
30+
EntityManagerFactory getEntityManagerFactory();
31+
32+
/**
33+
* @return metamodel describing managed types used in the persistence unit.
34+
*/
35+
Metamodel getMetamodel();
36+
}

0 commit comments

Comments
 (0)