15
15
*/
16
16
package org .springframework .data .jpa .repository .query ;
17
17
18
- import java .lang .reflect .Method ;
19
- import java .util .ArrayList ;
20
- import java .util .Collections ;
21
- import java .util .List ;
22
-
23
18
import jakarta .persistence .AttributeNode ;
24
19
import jakarta .persistence .EntityGraph ;
25
20
import jakarta .persistence .EntityManager ;
26
21
import jakarta .persistence .Query ;
27
22
import jakarta .persistence .Subgraph ;
28
23
24
+ import java .util .ArrayList ;
25
+ import java .util .Collections ;
26
+ import java .util .List ;
27
+
29
28
import org .springframework .data .jpa .repository .support .MutableQueryHints ;
30
29
import org .springframework .data .jpa .repository .support .QueryHints ;
31
30
import org .springframework .lang .Nullable ;
32
31
import org .springframework .util .Assert ;
33
- import org .springframework .util .ClassUtils ;
34
32
import org .springframework .util .ObjectUtils ;
35
- import org .springframework .util .ReflectionUtils ;
36
33
import org .springframework .util .StringUtils ;
37
34
38
35
/**
48
45
*/
49
46
public class Jpa21Utils {
50
47
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
-
64
48
private Jpa21Utils () {
65
49
// prevent instantiation
66
50
}
67
51
68
- public static QueryHints getFetchGraphHint (EntityManager em , @ Nullable JpaEntityGraph entityGraph ,
69
- Class <?> entityType ) {
52
+ public static QueryHints getFetchGraphHint (EntityManager em , JpaEntityGraph entityGraph , Class <?> entityType ) {
70
53
71
54
MutableQueryHints result = new MutableQueryHints ();
72
55
73
- if (entityGraph == null ) {
74
- return result ;
75
- }
76
-
77
56
EntityGraph <?> graph = tryGetFetchGraph (em , entityGraph , entityType );
78
57
79
- if (graph == null ) {
80
- return result ;
81
- }
82
-
83
58
result .add (entityGraph .getType ().getKey (), graph );
84
59
return result ;
85
60
}
@@ -94,24 +69,21 @@ public static QueryHints getFetchGraphHint(EntityManager em, @Nullable JpaEntity
94
69
* @param entityType must not be {@literal null}.
95
70
* @return the {@link EntityGraph} described by the given {@code entityGraph}.
96
71
*/
97
- @ Nullable
98
72
private static EntityGraph <?> tryGetFetchGraph (EntityManager em , JpaEntityGraph jpaEntityGraph , Class <?> entityType ) {
99
73
100
74
Assert .notNull (em , "EntityManager must not be null" );
101
75
Assert .notNull (jpaEntityGraph , "EntityGraph must not be null" );
102
76
Assert .notNull (entityType , "EntityType must not be null" );
103
77
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 ())) {
107
79
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 ) {}
114
84
}
85
+
86
+ return createDynamicEntityGraph (em , jpaEntityGraph , entityType );
115
87
}
116
88
117
89
/**
0 commit comments