Skip to content

Commit 7afb8d4

Browse files
committed
Polishing.
Remove JPA 2.1 guards as we require JPA 3.x. Refine API to reduce nullability. Original pull request: #3684 See #3682
1 parent f618000 commit 7afb8d4

File tree

4 files changed

+17
-45
lines changed

4 files changed

+17
-45
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private Query applyEntityGraphConfiguration(Query query, JpaQueryMethod method)
256256
JpaEntityGraph entityGraph = method.getEntityGraph();
257257

258258
if (entityGraph != null) {
259-
QueryHints hints = Jpa21Utils.getFetchGraphHint(em, method.getEntityGraph(),
259+
QueryHints hints = Jpa21Utils.getFetchGraphHint(em, entityGraph,
260260
getQueryMethod().getEntityInformation().getJavaType());
261261

262262
hints.forEach(query::setHint);

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/Jpa21Utils.java

+12-40
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import java.lang.reflect.Method;
19-
import java.util.ArrayList;
20-
import java.util.Collections;
21-
import java.util.List;
22-
2318
import jakarta.persistence.AttributeNode;
2419
import jakarta.persistence.EntityGraph;
2520
import jakarta.persistence.EntityManager;
2621
import jakarta.persistence.Query;
2722
import jakarta.persistence.Subgraph;
2823

24+
import java.util.ArrayList;
25+
import java.util.Collections;
26+
import java.util.List;
27+
2928
import org.springframework.data.jpa.repository.support.MutableQueryHints;
3029
import org.springframework.data.jpa.repository.support.QueryHints;
3130
import org.springframework.lang.Nullable;
3231
import org.springframework.util.Assert;
33-
import org.springframework.util.ClassUtils;
3432
import org.springframework.util.ObjectUtils;
35-
import org.springframework.util.ReflectionUtils;
3633
import org.springframework.util.StringUtils;
3734

3835
/**
@@ -48,38 +45,16 @@
4845
*/
4946
public class Jpa21Utils {
5047

51-
private static final @Nullable Method GET_ENTITY_GRAPH_METHOD;
52-
private static final boolean JPA21_AVAILABLE = ClassUtils.isPresent("jakarta.persistence.NamedEntityGraph",
53-
Jpa21Utils.class.getClassLoader());
54-
55-
static {
56-
57-
if (JPA21_AVAILABLE) {
58-
GET_ENTITY_GRAPH_METHOD = ReflectionUtils.findMethod(EntityManager.class, "getEntityGraph", String.class);
59-
} else {
60-
GET_ENTITY_GRAPH_METHOD = null;
61-
}
62-
}
63-
6448
private Jpa21Utils() {
6549
// prevent instantiation
6650
}
6751

68-
public static QueryHints getFetchGraphHint(EntityManager em, @Nullable JpaEntityGraph entityGraph,
69-
Class<?> entityType) {
52+
public static QueryHints getFetchGraphHint(EntityManager em, JpaEntityGraph entityGraph, Class<?> entityType) {
7053

7154
MutableQueryHints result = new MutableQueryHints();
7255

73-
if (entityGraph == null) {
74-
return result;
75-
}
76-
7756
EntityGraph<?> graph = tryGetFetchGraph(em, entityGraph, entityType);
7857

79-
if (graph == null) {
80-
return result;
81-
}
82-
8358
result.add(entityGraph.getType().getKey(), graph);
8459
return result;
8560
}
@@ -94,24 +69,21 @@ public static QueryHints getFetchGraphHint(EntityManager em, @Nullable JpaEntity
9469
* @param entityType must not be {@literal null}.
9570
* @return the {@link EntityGraph} described by the given {@code entityGraph}.
9671
*/
97-
@Nullable
9872
private static EntityGraph<?> tryGetFetchGraph(EntityManager em, JpaEntityGraph jpaEntityGraph, Class<?> entityType) {
9973

10074
Assert.notNull(em, "EntityManager must not be null");
10175
Assert.notNull(jpaEntityGraph, "EntityGraph must not be null");
10276
Assert.notNull(entityType, "EntityType must not be null");
10377

104-
Assert.isTrue(JPA21_AVAILABLE, "The EntityGraph-Feature requires at least a JPA 2.1 persistence provider");
105-
Assert.isTrue(GET_ENTITY_GRAPH_METHOD != null,
106-
"It seems that you have the JPA 2.1 API but a JPA 2.0 implementation on the classpath");
78+
if (StringUtils.hasText(jpaEntityGraph.getName())) {
10779

108-
try {
109-
// first check whether an entityGraph with that name is already registered.
110-
return em.getEntityGraph(jpaEntityGraph.getName());
111-
} catch (Exception ex) {
112-
// try to create and dynamically register the entityGraph
113-
return createDynamicEntityGraph(em, jpaEntityGraph, entityType);
80+
try {
81+
// check whether an entityGraph with that name is already registered.
82+
return em.getEntityGraph(jpaEntityGraph.getName());
83+
} catch (Exception ignore) {}
11484
}
85+
86+
return createDynamicEntityGraph(em, jpaEntityGraph, entityType);
11587
}
11688

11789
/**

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/DefaultQueryHints.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package org.springframework.data.jpa.repository.support;
1717

18+
import jakarta.persistence.EntityManager;
19+
1820
import java.util.Optional;
1921
import java.util.function.BiConsumer;
2022

21-
import jakarta.persistence.EntityManager;
22-
2323
import org.springframework.data.jpa.repository.EntityGraph;
2424
import org.springframework.data.jpa.repository.query.Jpa21Utils;
2525
import org.springframework.data.jpa.repository.query.JpaEntityGraph;
@@ -99,7 +99,7 @@ private QueryHints getFetchGraphs() {
9999
return Optionals
100100
.mapIfAllPresent(entityManager, metadata.getEntityGraph(),
101101
(em, graph) -> Jpa21Utils.getFetchGraphHint(em, getEntityGraph(graph), information.getJavaType()))
102-
.orElse(new MutableQueryHints());
102+
.orElseGet(MutableQueryHints::new);
103103
}
104104

105105
private JpaEntityGraph getEntityGraph(EntityGraph entityGraph) {

src/main/antora/modules/ROOT/pages/jpa/query-methods.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ public interface GroupRepository extends CrudRepository<GroupInfo, String> {
824824

825825
It is also possible to define ad hoc entity graphs by using `@EntityGraph`. The provided `attributePaths` are translated into the according `EntityGraph` without needing to explicitly add `@NamedEntityGraph` to your domain types, as shown in the following example:
826826

827-
.Using AD-HOC entity graph definition on an repository query method.
827+
.Using ad-hoc entity graph definitions on a repository query method
828828
====
829829
[source, java]
830830
----

0 commit comments

Comments
 (0)