|
| 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.Entity; |
| 19 | +import jakarta.persistence.EntityManagerFactory; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.LinkedHashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import org.jspecify.annotations.Nullable; |
| 30 | + |
| 31 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 32 | +import org.springframework.core.annotation.MergedAnnotation; |
| 33 | +import org.springframework.data.jpa.repository.EntityGraph; |
| 34 | +import org.springframework.data.jpa.repository.query.JpaQueryMethod; |
| 35 | +import org.springframework.data.repository.core.RepositoryInformation; |
| 36 | +import org.springframework.data.repository.query.ReturnedType; |
| 37 | +import org.springframework.util.StringUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * Factory for {@link AotEntityGraph}. |
| 41 | + * |
| 42 | + * @author Mark Paluch |
| 43 | + * @since 4.0 |
| 44 | + */ |
| 45 | +class EntityGraphLookup { |
| 46 | + |
| 47 | + private final EntityManagerFactory entityManagerFactory; |
| 48 | + |
| 49 | + public EntityGraphLookup(EntityManagerFactory entityManagerFactory) { |
| 50 | + this.entityManagerFactory = entityManagerFactory; |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + public @Nullable AotEntityGraph findEntityGraph(MergedAnnotation<EntityGraph> entityGraph, |
| 55 | + RepositoryInformation information, ReturnedType returnedType, JpaQueryMethod queryMethod) { |
| 56 | + |
| 57 | + if (!entityGraph.isPresent()) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + EntityGraph.EntityGraphType type = entityGraph.getEnum("type", EntityGraph.EntityGraphType.class); |
| 62 | + String[] attributePaths = entityGraph.getStringArray("attributePaths"); |
| 63 | + Collection<String> entityGraphNames = getEntityGraphNames(entityGraph, information, queryMethod); |
| 64 | + List<Class<?>> candidates = Arrays.asList(returnedType.getDomainType(), returnedType.getReturnedType(), |
| 65 | + returnedType.getTypeToRead()); |
| 66 | + |
| 67 | + for (Class<?> candidate : candidates) { |
| 68 | + |
| 69 | + Map<String, jakarta.persistence.EntityGraph<?>> namedEntityGraphs = entityManagerFactory |
| 70 | + .getNamedEntityGraphs(Class.class.cast(candidate)); |
| 71 | + |
| 72 | + if (namedEntityGraphs.isEmpty()) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + for (String entityGraphName : entityGraphNames) { |
| 77 | + if (namedEntityGraphs.containsKey(entityGraphName)) { |
| 78 | + return new AotEntityGraph(entityGraphName, type, Collections.emptyList()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (attributePaths.length > 0) { |
| 84 | + return new AotEntityGraph(null, type, Arrays.asList(attributePaths)); |
| 85 | + } |
| 86 | + |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + private Set<String> getEntityGraphNames(MergedAnnotation<EntityGraph> entityGraph, RepositoryInformation information, |
| 91 | + JpaQueryMethod queryMethod) { |
| 92 | + |
| 93 | + Set<String> entityGraphNames = new LinkedHashSet<>(); |
| 94 | + String value = entityGraph.getString("value"); |
| 95 | + |
| 96 | + if (StringUtils.hasText(value)) { |
| 97 | + entityGraphNames.add(value); |
| 98 | + } |
| 99 | + entityGraphNames.add(queryMethod.getNamedQueryName()); |
| 100 | + entityGraphNames.add(getFallbackEntityGraphName(information, queryMethod)); |
| 101 | + return entityGraphNames; |
| 102 | + } |
| 103 | + |
| 104 | + private String getFallbackEntityGraphName(RepositoryInformation information, JpaQueryMethod queryMethod) { |
| 105 | + |
| 106 | + Class<?> domainType = information.getDomainType(); |
| 107 | + Entity entity = AnnotatedElementUtils.findMergedAnnotation(domainType, Entity.class); |
| 108 | + String entityName = entity != null && StringUtils.hasText(entity.name()) ? entity.name() |
| 109 | + : domainType.getSimpleName(); |
| 110 | + |
| 111 | + return entityName + "." + queryMethod.getName(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments